History:
In Perl, variable scope can be limited through the my operator (lexical visibility) and local (dynamic temporary re-assignment). local is widely used to override global variables and special descriptors (like $_, $/, $@, %ENV).
Issue:
The main issue is the confusion between dynamic and lexical scope. local does not create a new variable but temporarily substitutes the value in a global (or package) variable within the execution of a block. This is particularly critical when re-assigning variables such as $/ (line separator), $_ (default variable), $^W (warning flag), %ENV, STDIN/STDOUT.
Solution:
Code example:
our $Global = "Hello!"; sub change1 { my $Global = "Bye!"; print "$Global "; } sub change2 { local $Global = "Bye!"; print "$Global "; } print "$Global "; # Hello! change1(); # Bye! print "$Global "; # Hello! change2(); # Bye! print "$Global "; # Hello!
Key features:
Can local be applied to lexical variables declared with my?
No, local works only with package-global variables. It is powerless on my-objects.
What happens when applying local to special descriptors like STDIN?
You can temporarily re-assign STDIN/STDOUT using local, for example, to substitute the input/output stream in a subroutine without a global effect. After exiting the block, the handle will be restored.
What is the critical difference between local and my in recursive function calls?
local provides a "push/pop" stack of values — each call temporarily redefines the package value, and nested calls receive this overridden value. my provides a single lexical value within the block without inheritance inside.
In a test, local is used to substitute %ENV, and after exiting the block, unexpected side effects occur in other threads because the code is multi-threaded and local was not applied correctly.
Pros:
Cons:
Special variables ($/, $@, $SIG) are substituted only for the duration of the necessary block call, after which the changes are correctly rolled back.
Pros:
Cons: