Here is a sample program that illustrates how parameters are passed to subroutines. You can also get the file here.
#!/usr/bin/perl -w
# This subroutine illustrates how Perl
# passes parameters to subroutines
sub param_test {
my( $m, $n ) = @_;
$p = \@_;
$p0 = \$_[0];
$p1 = \$_[1];
$mp = \$m;
$np = \$n;
print "p = $p\n"; # Prints the address of the local array @_
print "mp = $mp\n"; # Prints the address of m
print "np = $np\n"; # Prints the address of n
print "p0 = $p0\n"; # Note that $_[0] is an alias for x
print "p1 = $p1\n"; # Note that $_[1] is an alias for y
$_[0] = 9; # so changing $_[0] changes x as a side-effect
$n = 11; # but changing n has no side-effect
}
$x = 5;
$y = 7;
$xp = \$x;
$yp = \$y;
print "xp = $xp\n"; # Prints the address of x
print "yp = $yp\n"; # Prints the address of y
¶m_test( $x, $y );
print "x = $x\n"; # Note that x has been changed
print "y = $y\n"; # but that y stayed the same
Here is its output (the values printed will vary from machine to machine, but note which are the same and which are different):
xp = SCALAR(0x180d1b0) yp = SCALAR(0x180d0fc) p = ARRAY(0x180d1bc) mp = SCALAR(0x180b590) np = SCALAR(0x180b5a8) p0 = SCALAR(0x180d1b0) p1 = SCALAR(0x180d0fc) x = 9 y = 7