Professional Documents
Culture Documents
Introduction
Perlallowstheusertodefinetheirownfunctions,calledsubroutines.Theymaybeplacedanywherein
yourprogrambutit'sprobablybesttoputthemallatthebeginningorallattheend.
Asubroutinehastheformregardlessofanyparametersthatwemaywanttopasstoit:
subNAMEBLOCK
submysubroutine
{
print"Notaveryinterestingroutine\n";
print"Thisdoesthesamethingeverytime\n";
}
Allofthefollowingwillworktocallthissubroutine.Noticethatasubroutineiscalledwithan&character
infrontofthename:
&mysubroutine;
#Callthesubroutine
&mysubroutine($_); #Callitwithaparameter
&mysubroutine(1+2,$_);
#Callitwithtwoparameters
Whenthesubroutineiscalled,parametersarepassedasalistinthespecial@_listarrayvariable.This
variablehasabsolutelynothingtodowiththe$_scalarvariable.Example:
#!/usr/bin/perlw
#subroutinebasics
######################################################
subhello_string{
return"Helloworld!\n";
}
printhello_string;
######################################################
######################################################
subprintargs{
print"@_\n";
}
&printargs("Iam","theking.");
&printargs("Youare","mysubject.");
######################################################
######################################################
subsay_hello_to{
my$name=shift;
print"Hello,$name!\n";
}
#variouswaystocalltheabovesub
say_hello_to("Ramesh");
say_hello_to"Ramesh";
&say_hello_to("Ramesh");
######################################################
######################################################
submy_sub{
print"Hello,world!\n";
}
my$ref_to_sub=\&my_sub;
$ref_to_sub>();
#printsHello,world!
######################################################
######################################################
subsoldier{
$n+=1;#globalvariable$n
print"Hello,soldiernumber$n!\n";
}
&soldier;
&soldier;
&soldier;
######################################################
That print statement is just a debugging aid, so you can see that you called the subroutine. You normally
take in those sorts of statements when youre ready to deploy your program. But suppose you added
another printto the end of the subroutine, like this (subroutineoops.pl):
subsum_of_fred_and_barney{
print"Hey,youcalledthesum_of_fred_and_barneysubroutine!\n";
$fred+$barney;#That'snotthereturnvalue
print"Hey,I'mreturningavaluenow!\n";#Oops!
}
You get:
Uselessuseofaddition(+)invoidcontextatsubreturnoops.plline5.
Hey,youcalledthesum_of_fred_and_barneysubroutine!
Hey,I'mreturningavaluenow!
$wilmais1.
Hey,youcalledthesum_of_fred_and_barneysubroutine!
Hey,I'mreturningavaluenow!
$bettyis3.
The last expression evaluated is not the addition anymore; its now the print statement, whose return
value is normally 1 , meaning printing was successful, but thats not the return value you actually
wanted. So be careful when adding additional code to a subroutine, since the last expression evaluated
will be the return value. The term void context is just a fancy way of saying that you arent using the
answer, whether that means storing it in a variable or using it any other way.
The last evaluated expression really means the last expression that Perl evaluates, rather than the last
statement in the subroutine. For example, this subroutine returns the larger value of $fredor $barney:
sublarger_of_fred_or_barney{
if($fred>$barney){
$fred;
}else{
$barney;
}
}