|
In Perl, arguments are
normally passed by reference. This means that if you
modify a member of the array @_, you are
actually modifying the original value you passed to
the function.
sub foo { $_[0] ++ }
$a = 25;
foo($a);
print "$a\n";
This prints 26. Perl subroutines are often written
this way:
sub foo {
my ($arg) = @_;
$arg ++
}
This copies the parameters into lexical
(scope-local) variables. In this case, $a
won't be incremented—only the lexical variable
$arg will. For people coming from a C
background this is more intuitive, emulating the
pass-by-value practice of most programming
languages.
In my parser, the first form is used so that we
can maintain the regex position across function
calls. It also avoids copying the string over and
over. I recommend using the second form most of the
time, but remembering the first form for when the
need arises.
For more information, see the
perlsub documentation.
|