Perl Tutorials - Herong's Tutorial Examples - v6.03, by Herong Yang
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) HerongYang.com. All Rights Reserved.
#
$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.
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
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
Name Spaces and Perl Module Files
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
Open Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
SOAP::Lite - SOAP Server-Client Communication Module
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary