Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Understanding why strict prevents a use of local

by IraTarball (Monk)
on Oct 20, 2001 at 01:10 UTC ( [id://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.

Ira,

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found