How to use subroutines in perl?
Try this:
For Subroutines
Â
Like any sensible programming language Perl permits the user to outline their own functions, referred to as subroutines. they will be placed anywhere in your program however it's in all probability best to place all of them at the start or all at the top. A subroutine has the shape.
Â
sub printargs
{
print "@_n";
}
Â
&printargs("perl", "king"); # Example prints "perl king"
&printargs("frog", "and", "toad"); # Prints "frog and toad"
Â
Â
Regardless of any parameters that we may want to pass to it. All of the following will work to call this subroutine. Notice that a subroutine is called with an & character in front of the name:
Â
&mysubroutine; # Call the subroutine
&mysubroutine($_); # Call it with a parameter
&mysubroutine(1+2, $_); # Call it with two parameters
In the higher than case the parameters are acceptable however ignored. When the subroutine is named any parameters are passed as a listing within the special @_ list array variable. This variable has completely nothing to try to with the $_ scalar variable. the subsequent subroutine just prints out the list that it had been referred to as with. it's followed by a handful of samples of its use.
sub printargs
{
print "@_n";
}
Â
&printargs("perly", "king"); # Example prints "perly king"
&printargs("frog", "and", "toad"); # Prints "frog and toad"
Just like the other list array the individual parts of @_ is accessed with the sq. bracket notation:
sub printfirsttwo
{
print "Your first argument was $_[0]n";
print "and $_[1] was your secondn";
}
Â
Â
Again it ought to be stressed that the indexed scalars $_[0] and $_[1] and thus on don't have anything to with the scalar $_ which may even be used while not concern of a clash.
Â
Â
For returning values:
Â
Result of a subroutine is usually the last item evaluated. This subroutine returns the most of 2 input parameters.
An example of its use follows.
Â
Sub maximum
{
if ($_[0] > $_[1])
{
$_[0];
}
else
{
$_[1];
}
}
Â
$biggest = &maximum(37, 24); # Now $biggest is 37
Â
The &printfirsttwo subroutine higher than additionally returns a worth,during this case one.
this can be as a result of the last item that subroutine did was a print statementÂ
and therefore the results of a successful print statement is usually one.
Â
For local variables:
Â
The @_ variable is native to the present subroutine, and thus after all are $_[0], $_[1], $_[2],Â
and so on. alternative variables is created native too, and this can be helpful if we wish toÂ
begin altering the input parameters. the subsequent subroutine tests to examine if one string isÂ
within another, areas not withstanding. An example follows.
Â
sub inside
{
local($a, $b); # Make local variables
($a, $b) = ($_[0], $_[1]); # Assign values
$a =~ s/ //g; # Strip spaces from
$b =~ s/ //g; # Â local variables
($a =~ /$b/ || $b =~ /$a/); # Is $b inside $a
# Â or $a inside $b?
}
Â
&inside("lemon", "dole money"); # true
Â
Enable keep the basis phrase
Â
local($a, $b) = ($_[0], $_[1]);
Hope this helps.