Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Understanding why strict prevents a use of local

by IraTarball (Monk)
on Oct 20, 2001 at 01:10 UTC ( #120151=note: print w/replies, xml ) Need Help??

in reply to Understanding why strict prevents a use of local

Just a comment. BTW I agree with the above comments that you whould avoid globals <e>especially</e> in long scripts.

But as and aside regarding use vars and  our declarations. I don't believe they are quite equivalent. This might be splitting hairs, but a use vars declaration gives a package variable while an our declaration gives a global disguised as a my variable. This means the scoping is lexical for our variables. The following code

&decl(); print $variable; sub decl { our $variable = 1; }
would not compile under strict. $variable is out of scope by the print statement. A use vars qw($variable); line would work however.

Another implication, if you are using packages to give different name spaces in the same file, a declaration like...

package one; use vars qw($thing1); our $thing2; #stuff; package main; print "$thing1 and $thing2";
might not do what you expect. For example not run under strict. The vars variable $thing1 is a package variable, not available to the main package without a $one::thing1 type address. $thing2 however, the our declaration <e>is</e> available to the main package. It's scope is the same as my so is file wide in this sample.

I didn't get this when I first started using <cod>our</code> so I just wanted to throw it there since it seems to apply to this thread.


"So... What do all these little arrows mean?"

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-05-20 18:31 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (128 votes). Check out past polls.