|
|
| Keep It Simple, Stupid | |
| PerlMonks |
Re: Initialize variable in BEGINby LanX (Saint) |
| on Apr 30, 2025 at 15:37 UTC ( [id://11164861]=note: print w/replies, xml ) | Need Help?? |
|
our was introduced¹ into Perl to explicitly have the same lexical scoping rules like my but now for package variables. Hence you are declaring 3 different² variables in your code. Since declarations happen at compile time it's safe to have only one in the surrounding scope (block or file) This will cover all nested scopes. Like demonstrated by hippo in the other reply on the file scope.
Cheers Rolf
Updates¹) See https://perldoc.perl.org/5.6.0/perldelta#%22our%22-declarations ²) That's probably confusing, three different lexical aliases are declared, but all store inside the same package variable. It's just that the values are switched whenever the scope is changing, giving the appearance of different variables. Similar to local
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||