1 / 7

Lecture 8:

Lecture 8: . Basic concepts of subroutines. Functions . In perl functions take the following format: sub subname { my $var1 = $_[0]; statements Return value } After all the subs are defined they are then called as required: my $variable = subname ($name); The SubroutineExample.pl.

zayit
Download Presentation

Lecture 8:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8: Basic concepts of subroutines

  2. Functions • In perl functions take the following format: • sub subname • { • my $var1 = $_[0]; • statements • Return value • } • After all the subs are defined they are then called as required: • my $variable = subname($name); • The SubroutineExample.pl

  3. Functions: passing more than one variable • Subroutines : pass one or more variables. • sub subname • { • my ($var1, $var2,….) = @_; • { alternatively uses subscripts $_[0], $_[2].…. • statements • Return value • } • After all the subs are defined they are then called as required: • my $variable = subname($name); • The SubroutineExample2.pl

  4. Returning values • A basic return uses the “return $var1” / “return @sequences”…. • The values can also be returned if they are the last statement in the program. • Sub-routine biggest{ • My ($a, $b, $c) = @_; # the list of “local varaible” must correspond to the types that are passed. If you pass a hash table my(%hash) = @_ • use strict; use warnings; # this is for the local variable • My $temp; • $temp = ($a<$b ? $a : $b); #(An alternative if else statement) • # returning the value (last statement in the function) • if ($temp < $c ) • { &temp} # this is returned • Else • {$c} #this is returned • } • In main • print “The biggest of the three number is: biggest(5,2,4)”

  5. Passing by reference • By using the \ in a function call you pass the pointer to the subroutine • The pointer can be de-referenced by using the $ value • sub ByReference (PassReference.pl) • { • print " In sub5: parms = @_ \n"; • my($val1) = $_[0]; • $arry_ptr = $_[1]; • $var2_ptr = $_[2]; • print " In sub5: \$val1 = ", $val1, " Address = ", \$val1, "\n"; • $val1 = $val1 - 1; • print " In sub5: Var1 = ", $val1, "\n"; • print " In sub5: Var2 = ", $var2_ptr, " Contents = ", $$var2_ptr, "\n"; • $$var2_ptr = $$var2_ptr - 1; • print " In sub5: Variable1 = ", $var2_ptr, " Contents = ", $$var2_ptr, "\n"; • chop(@$arry_ptr); • print " In sub5: Array1 = ", $arry_ptr, " Contents = ", @$arry_ptr, "\n"; • } • In main • ByReference($var1, \@arr1, \$var2); (what are the values of output)

  6. Exercises • Write a program using subroutines that: • Confirms if the user has input the code in the following format: • Classcode_yearcode(papercode) • E.g dt249 4(w203c) • Modify the sequence size example (from “basic pattern matching” lecture) • Allow the user to input a file name and determine its length. • Write the script using one or more subroutines.

  7. Exercises • Write a sub-routine that can find the reverse complement of an DNA sequence. Use this subroutine to print out the compliment of each sequence line of a DNA fastafile • write a subroutine to convert a DNA sequence to amino acid sequence; use this to translated all three reading frames in a fasta file.

More Related