Notes on Lexical (my) Variables
Those lexical variables can actually be used in any block, not
merely in a subroutine’s block. For example, they can be used in the
block of an if, while, or foreach:
foreach (1..10) {
my($square) = $_ * $_; # private variable in this loop
print "$_ squared is $square.\n";
}The variable $square is private
to the enclosing block; in this case, that’s the block of the foreach loop. If there’s no enclosing block,
the variable is private to the entire source file. For now, your
programs aren’t going to use more than one source file, so this isn’t an
issue. But the important concept is that the
scope of a lexical variable’s name is limited to
the smallest enclosing block or file. The only code
that can say $square and mean that
variable is the code inside that textual scope. This is a big win for
maintainability—if the wrong value is found in $square, the culprit will be found within a
limited amount of source code. As experienced programmers have learned
(often the hard way), limiting the scope of a variable to a page of
code, or even to a few lines of code, really speeds along the
development and testing cycle.
Note also that the my operator
doesn’t change the context of an assignment:
my($num) = @_; # list context, same as ($num) = @_; my $num = @_; # scalar context, same as $num = @_;
In the first one, $num gets the first parameter, as a list-context assignment; in the second, it gets the number of parameters, in a scalar context. Either line of code ...