Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

SubParamList.pl - Example on Parameter List

This section provides a tutorial example on how the parameter list is passed to and received in a user defined subroutine by the local array variable @_.

As mentioned in the previous section, 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.

All parameters of subroutine call will be received as elements in a local array variable named as @_.

Here is a tutorial example on parameter list is passed to and received in a subroutine:

#- SubParamList.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   $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";
}

Here is the output of the tutorial script:

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.

Sections in This Chapter

Declaring and Calling Subroutines

SubParamList.pl - Example on Parameter List

SubParamAlias.pl - Example on Parameters as Alias

SubReturnValue.pl - Example on Return Values

SubCalling.pl - Example on Calling Formats

Dr. Herong Yang, updated in 2008
SubParamList.pl - Example on Parameter List