⭐ in reply to What is the difference between 'local' and 'my'?
Suppose you have the following code:
you would get: 7 - 3 - 7 - 3our $a = 3; sub f { print "$a\n"; } sub g { my $a = 7; print "$a\n"; &f (); print "$a\n"; } &g (); print "$a\n";
If you substitute the my in 'g' by local, you get: 7 - 7 - 7 - 3
Internally, the difference in the implementation is that local stores the variable value in a stack for the duration of the scope, and restores it at the end of the scope, while my actually creates a new variable which hides the outer one for the duration of the scope where it is defined, but not for other scopes which may be invoked. creates
|
|---|