By default, all variables in Perl are global variables; that is, they
are accessable from every part of the program. But you can create
private variables called lexical
variables at any
time with the my operator:
The subroutine in the previous example could be made even simpler.
Did you notice that the list ($a,
$b) was written twice? That my
operator can also be applied to a list of variables enclosed in
parentheses, so it's more customary to combine those first two
statements in the subroutine:
my($a, $b) = @_; # Name the subroutine parameters
That one statement creates the private variables and sets their
values, so the first parameter now has the easier-to-use name
$a and the second has $b.
Nearly every subroutine will start with a line much like that one,
naming its parameters. When you see that line, you'll know that
the subroutine expects two scalar parameters, which we'll call
$a and $b inside the
subroutine.
 |  |  |
4.5. Arguments |  | 4.7. The local Operator |