Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Scope of lexical variables in the main script

by AnomalousMonk (Archbishop)
on Apr 20, 2012 at 19:33 UTC ( [id://966246]=note: print w/replies, xml ) Need Help??


in reply to Scope of lexical variables in the main script

The interleaving of the definition of 'global' variables (either lexical or package) with the invocation of functions that access them has yet another tricky aspect.

Variables are defined at compile time, but not initialized until run time (unless explicitly initialized in something like a  BEGIN or  INIT block or through some other compile-time mechanism such as the use built-in). This leads to situations in which a function can access a variable that has been created and exists (as proven by the fact that the code runs under strictures), but has no defined value. Invocation of the same function at a different point in the code will find the function accessing a defined value. All the more reason to be very circumspect about the use of global variables.

>perl -wMstrict -le "func('before'); my $tricky = 'initialized at runtime'; func('after'); ;; sub func { printf qq{$_[0]: %s defined: }, defined($tricky) ? 'is' : 'NOT'; print qq{'$tricky'}; } " Use of uninitialized value $tricky in concatenation (.) or string ... before: NOT defined: '' after: is defined: 'initialized at runtime'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2026-03-06 12: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.