/
Subroutines  getting efficient with Perl Environmental Genomics Thematic Programme Data Subroutines  getting efficient with Perl Environmental Genomics Thematic Programme Data

Subroutines getting efficient with Perl Environmental Genomics Thematic Programme Data - PDF document

pamella-moone
pamella-moone . @pamella-moone
Follow
438 views
Uploaded On 2014-12-13

Subroutines getting efficient with Perl Environmental Genomics Thematic Programme Data - PPT Presentation

noxacuk Bela Tiwari btiwaricehacuk Subroutines get efficient So far The code we have looked at so far has been sequenti al do this do that now do something finish Problem You need something to be done over and over perhap s slightly differently depe ID: 23093

noxacuk Bela Tiwari btiwaricehacuk Subroutines

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Subroutines getting efficient with Perl..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Subroutines –getting efficient with Perl�-45/Environmental Genomics Thematic ProgrammeData Centrehttp://envgen.nox.ac.ukBela Tiwaribtiwari@ceh.ac.ukSubroutines –get efficientSo far:The code we have looked at so far has been sequential:do this;do that;now do something;finish;ProblemYou need something to be done over and over, perhaps slightly differently depending on the contextSolutionPut the code in a subroutine and call the subroutine whenever needed.Subroutines –get efficientA subroutine is a named block of codethat can be executed as many times as you wish. An artificial example:Instead of:print “Hello everyone!”;You could use:subhello_sub print "Hello everyone!\n“;}#subroutine definition&hello_sub; #call the subroutineSyntax: There are a number of correct ways you can define and use subroutines. One is:#!/usr/bin/perlsome code here;some more here;lalala();declareand callthe subroutinea bit more code here;exit();#explicitly exit the program############sub lalala {definethe subroutinecode to define what lalala does;#code defining the functionality of lalalamore defining lalala;return();#end of subroutine –return to the programSyntax: Outline review of previous slide:#!/usr/bin/perllalala();#call the subroutine###########sub lalala {#define the subroutinereturn();#end of subroutine –return to the programSubroutines –get efficientSyntax:Permutations on the theme:Defining the whole subroutine within the script when it is firstneeded:sub hello_sub {print “Hello everyone\n”;}The use of an ampersand to call the subroutine:&hello_sub;Note: There are subtle differences in the syntax allowed and required by Perl depending on how you declare/define/call your subroutines. Subroutines –get efficientParameters and Return ValuesParametersgiving the subroutine values/information to work withgiven by putting the required information in between the subroutine brackets when the subroutine is called ()e.g. lalala($firsthing, $secondthing)Return valuesgetting information out of the subroutinesubroutines always return a valueif you provide a return statement with no explicit value to send back, it will return true (1) if it succeededif you provide no return statement, the subroutine will return the value of the last statement it evaluatedyou can program subroutines to return values of your choice#!/usr/bin/perlmy $thing1 = “something”;my $thing2 = “anotherthing”;some code here;some more here;my $whatIwant = lalala($thing1, $thing2); #call the subroutine here#and put whatever is returned into #the variable $whatIwanta bit more code here;exit();#explicitly exit the program#########sub lalala {#define the subroutinecode to get the values of $thing1 and $thing2 into subroutine variablescode to define what lalala does;#code defining lalalacode to give a value to a variable called $outputreturn($output);#end of subroutine –return the value $outputmy $whatIwant = lalala($thing1, $thing2);#########sub lalala {code to get the values of $thing1 and $thing2 into subroutine variablesreturn();$whatIwant = lalala($thing1, $thing2);$thing1 and $thing2 are now in an array called @_$thing1 is now $_[0] #the first element of @_$thing2 is now $_[1]#the second element of @#########sub lalala {#need to retrieve the valuesfrom @_ array and put them into variables #within our subroutinemy $funcvar1 = shift (@_);#take the first element of the array (has the same valueas $thing1)my $funcvar2 = shift (@_);#take the second element of the array (the same value as $thing2)return();$whatIwant = lalala($thing1, $thing2);#########sub lalala {my $funcvar1 = shift;#where is @_#$funcvar1 has the value of $thing1my $funcvar2 = shift;#where is @_#$funcvar2 has the value of $thing2return();########################################################Even easier:my ($funcvar1, $funcvar2) = @_;$whatIwant = lalala($thing1, $thing2);Hey, wait! Does that look familiar?$seq_length = length($seq);$last_thing = pop(@array1);Functions are really built-in subroutines!!Subroutines are just you writing your own functions! (or using someone else’s functions) Subroutines so far:What a subroutine isHow to call a subroutineHow to pass parameters into a subroutineNow:More on getting values out of a subroutineMore on getting values out of a subroutineRecall:Return valuesgetting information out of the subroutinesubroutines always return a valueif you provide a return statement with no explicit value to send back, it will return true (1) if it succeededif you provide no return statement, the subroutine will return the value of the last statement it evaluatedyou can program subroutines to return values of your choice##############sub lalala {code to define what lalala does;more defining lalala;return();# if lalala executes successfully, this would return the value 1to your main programMore on getting values out of a subroutineYou can return a single value from a subroutine:sub lalala {code to give a value to a variable called $outputreturn($output);#end of subroutine –return the valueof $outputMore on getting values out of a subroutineIf you do not specify a return statement, the subroutine will return the result of the final statement evaluated:sub lalala {bunchacode;morecode;$total = $thing1 + $thing2; # $total will be returned in this caseEven more on getting values out of a subroutineYou can return a single value from a subroutine.You can return multiple values from a subroutine using a list. E.g. return (@array_of_values);return ($val1, $val2);An Aside:When you are returning something very large, you can just give its address, rather than pass the whole thing in or out of the subroutine.That is, you can specify the locationof values instead of their names.In other words, if you return thelocation of an array, you can use thislocationto access all the elementsof that arrayMore on “my”, variables and subroutinesSo far we have declared our variables using “my” So far we have written “use strict” at the top of our scriptsSo far we have glossed over why we do this…. More on “my”, variables and subroutinesVariables declared using myare visible only to the blockof code{ } they are in.So, if you declare them at the top of a script, the whole scriptcan see them.If you declare them inside a subroutine, only the subroutinecan see them.The term “lexical scoping” essentially refers to coding such that variables are “seen” only where they should be, so that changing the values of variables in certain parts of your program doesn’t inadvertently affect what happens to those variables in other parts of your program.#!/usr/bin/perl#simplesubfunc.pluse strict;use warnings;my ($number1, $number2);#declare scalar variablesprint "Enter a number: ";#mainchomp ($number1 = (STDIN&#x-1.3;捡));#bodyprint "Enter another number :";#ofchomp ($number2 = (STDIN&#x-1.3;捡));#codemy $total = addition_sub($number1, $number2);#go to subroutine#$total collects return valueprint "$number1 plus $number2 equals $total\n";exit();##########sub addition_sub {my $number3 = $number1 + $number2; #declare a variable and add the numbersreturn ($number3);#$number3 is only “seen” by the subroutine#return valueof $number3 from subroutine#!/usr/bin/perl#simplesubfunc.pluse strict;use warnings;my ($number1, $number2);#declare scalar variablesprint "Enter a number: ";#mainchomp ($number1 = (STDIN&#x-1.3;晙));#bodyprint "Enter another number :";#ofchomp ($number2 = (STDIN&#x-1.3;晙));#codemy $total = addition_sub($number1, $number2);#go to subroutine#$total collects return valueprint "$number1 plus $number2 equals $total\n";exit();##########sub addition_sub {my $number3 = $number1 + $number2; #declare a var and add the numbers#$number3 is only “seen” by the subroutine#no explicit return statement#!/usr/bin/perl #simplesubfunc2.pluse strict;use warnings;my ($number1, $number2, $number3);#now $number3 seen by #whole programprint "Enter a number: ";chomp ($number1 = (STDIN&#x-1.3;捡));print "Enter another number :";chomp ($number2 = (STDIN&#x-1.3;捡));addition_sub();#nothing catches the return value#don’t need $number1 or $number2 b/c sub knows about themprint "$number1 plus $number2 equals $number3\n";exit();##########sub addition_sub {$number3 = $number1 + $number2; #don’t declare $number3 with my#no explicit return statementMore on “my”, variables and subroutines –catinhat.pl#!/usr/bin/perluse strict;use warnings;my $catinhat = 5;# declare $catinhat in main programprint “\nThe catinhat in my main program is $catinhat\n”;subrout($catinhat);#pass value of main prog $catinhat to subroutprint “\nThe catinhat in my main program is still equal to $catinhat\n”;exit();###########sub subrout {my $catinhat = shift;#declare $catinhat in subroutineprint “\nThe catinhat in my subroutine is $catinhat\n”;$catinhat++;#play with subrout’s $catinhatprint “\The catinhat in my subroutine is now $catinhat\n”;return();More on “my”, variables and subroutines –catinhat2.plRecall:Variables declared using myare visible only to the block of codethey are in.Thus –true for subroutines, but also foreach loops, while loops, if/else statements–anything where a block of code { } is defined.#!/usr/bin/perluse strict;use warnings;my $catinhat = 5; #declare and give value 5 to main prog’s $catinhatmy @numarray = (1,2,3,4,5);print “\nThe catinhat in my main program is $catinhat\n”;foreach my $catinhat (@numarray) #declare foreach block’s $catinhat$catinhat +=5;#play with foreach block’s $catinhatprint “The number if the foreachloop is now $catinhat\n”;print “\nThe catinhat in my main program is still equal to $catinhat\n”; exit(); Overview of this sessionSubroutines are a block of code that (should) carry out particular tasks.Subroutines can be called as many times as required within your script.Some benefits of writing the functionality of your script into subroutines are:saves typing the same code over and overdecreases the possibility of errors creeping into the coderesults in far more maintainable codeis the first step towards creating code that can be reused in other scripts easilyYou can pass information into a subroutine and collect information from a subroutine.Values passed into a subroutine are held in the @_ array.You must have containers, (i.e. variables), to catch the values that you return from a subroutine.There is some syntactical variation in how you can call a subroutine. One common way is to call the subroutine in your main code, and define the subroutine at the end of the script.