Subroutines
With Perl, as with many other languages,
we can define our own functions, called subroutines
Example:
sub subroutine1
{
print "perl and scripts \n";
}
&subroutine1;
$p1 = 444;
sub subroutine2($p1)
{
print "$p1 is a parameter\n";
}
&subroutine2($p1);
$_ = "is a script language..";
sub subroutine3($_)
{
print "Perl $_\n";
}
&subroutine3($_);
$_ = 334;
$a = 233;
$b = "this chance ..";
sub subroutine4($a,$b)
{
print "$a is number\n";
print "$b is a string.\n";
}
&subroutine4($a, $b);
perl and scripts
444 is a parameter
Perl is a script language..
233 is number
this chance .. is a string.
Other example:
--------------
A subroutine can retur a value:
sub min
{
if ($_[0] <$_[1])
{
$_[0];
}
else
{
$_[1];
}
}
$the_min = &min(-123, 55);
print $the_min;
that returs -123
Other example:
-------------
sub print_args
{
print "@_\n";
}
&print_args("best", "regards.");
outputs
best regards.
|