local
local EXPRThis operator does not create a local variable; use my for that. Instead, it localizes existing
variables; that is, it causes one or more global variables to have
locally scoped values within the innermost enclosing block, eval, or file. If more than one variable is
listed, the list must be placed in parentheses because the operator
binds more tightly than commas. All listed variables must be legal
lvalues—that is, something you could assign to; this can include
individual elements of arrays or hashes.
This operator works by saving the current values of the specified
variables on a hidden stack and restoring them on exiting the block,
subroutine, eval, or file. After the
local is executed, but before the
scope is exited, any subroutines and executed formats will see the
local, inner value, instead of the previous, outer value, because the
variable is still a global variable, despite having a localized value.
The technical term for this is dynamic scoping. See
the section Scoped Declarations in Chapter 4.
The EXPR may be assigned to if desired,
which lets you initialize your variables as you localize them. If no
initializer is given, all scalars are initialized to undef, and all arrays and hashes to (). As with ordinary assignment, if you use
parentheses around the variables on the left (or if the variable is an
array or hash), the expression on
the right is evaluated in list context. Otherwise, the expression on the
right is evaluated in scalar context.
In any event, ...