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.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

User Defined Subroutines

 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

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 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 Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 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

 References

 PDF Printing Version