Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Initialize variable in BEGIN

by LanX (Saint)
on Apr 30, 2025 at 15:37 UTC ( [id://11164861]=note: print w/replies, xml ) Need Help??


in reply to Initialize variable in BEGIN

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
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11164861]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2026-05-12 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.