|
perlsub - Perl Subroutines
This chapter describes:
- Rules on Perl subroutines.
- Examples of passing parameters, return values, and calling formats.
Perl Subroutine Rules
Like many other programming languages, Perl allows you
to define and call your own subroutines. But some
of the rules are unusual comparing to other languages.
1. All parameters of a subroutine call are passed as a single list
of scalars. If a list (array or hash) is used in a subroutine call
as a parameter, it will be exploded into multiple parameters with
its elements.
2. All parameters will be received as elements in a local array
named as @_.
3. All parameters are passed as aliases. If the value of a parameter
is modified inside a subroutine, the value of the corresponding variable
in the calling subroutine is also modified.
But if the calling subroutine is passing a literal as a parameter,
then you can not modify the value of this parameter.
You will get an execution error.
4. All subroutines return values. So in Perl subroutines are really
functions. If a subroutine is ended with no explicit return statement,
the value of the last expression will be returned.
5. All return values are passed back to the caller as a single list
of scalars. If a list (array or hash) is used as a return value,
it will be exploded into multiple return values with its elements.
6. There several ways to call a subroutine:
- "&sub_name(list)" - Calling a subroutine with the specified list of
parameters.
- "sub_name(list)" - Same as "&sub_name(list)".
- "sub_name list" - Same as "&sub_name(list)", if sub_name is pre-defined
as a subroutine.
- "sub_name" - Same as "&sub_name()", if sub_name is pre-defined
as a subroutine.
- "&sub_name" - Same as "&sub_name(@_)". This one is very un-usual.
Be careful.
SubParamList.pl - Example on Parameter List
#- SubParamList.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
$a = 3.14;
@a = ('mon','tue','wed','thu','fri');
$a{'mon'} = 1;
$a{'tue'} = 2;
$a{'wed'} = 3;
&subParamList($a);
&subParamList(@a);
&subParamList(%a);
&subParamList($a,@a,%a);
exit;
sub subParamList {
print join(',',@_), "\n";
}
Output:
3.14
mon,tue,wed,thu,fri
wed,3,mon,1,tue,2
3.14,mon,tue,wed,thu,fri,wed,3,mon,1,tue,2
The output clearly shows you that array and hash are indeed replaced
by their elements as multiple parameters.
SubParamAlias.pl - Example on Parameter Passing
#- SubParamAlias.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
$a = 'hello';
@a = ('mon','tue','wed','thu','fri');
# &subParamAlias('world');
&subParamAlias($a);
print $a, "\n";
&subParamAlias(@a);
print join(',',@a), "\n";
exit;
sub subParamAlias {
foreach (@_) {
tr/a-z/A-Z/;
}
}
Output:
HELLO
MON,TUE,WED,THU,FRI
The output clearly shows you that modifying the parameter array directly
will cause value changes on the caller's variables.
SubReturnValue.pl - Example on Return Values
#- SubReturnValue.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
$a = 'hello';
@a = ('mon','tue','wed','thu','fri');
print &subReturnValue($a), "\n";
print $a, "\n";
print join(',',&subReturnValue(@a)), "\n";
print join(',',@a), "\n";
exit;
sub subReturnValue {
my @l = @_;
foreach (@l) {
tr/a-z/A-Z/;
}
return @l;
}
Output:
HELLO
hello
MON,TUE,WED,THU,FRI
mon,tue,wed,thu,fri
The output clearly shows you that an array can be easily returned to the caller.
SubCalling.pl - Example on Calling Format
#- SubCalling.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
sub subCalling {
print join(',',@_), "\n";
}
&subCalling('mon','tue','wed');
subCalling('jan','feb','mar');
subCalling 'can','usa','chn';
subCalling;
@_ = ('1st','2nd','3rd');
&subCalling;
exit;
Output:
mon,tue,wed
jan,feb,mar
can,usa,chn
1st,2nd,3rd
|