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

Re: my and local variable significance in a particular case

by JavaFan (Canon)
on Apr 24, 2012 at 07:33 UTC ( #966753=note: print w/ replies, xml ) Need Help??


in reply to my and local variable significance in a particular case

Suppose I have perl script which has no 'use strict'/'use warnings' in it. In that case what is the significance of a 'my' and 'local' variable in it.
You seem to be under the impression that "strict" and "my" are related. They are not, although if you read Perlmonks you may get the impression they are.

my creates a lexical variable. It's the only way to create a lexical variable, and the variable is always lexical. Whether or not strict or warnings are enabled.

local creates a temporary new value for a package variable (technically, the implementation is slightly different, but for most practical purposes, you won't notice) -- the old value is restored when leaving the current, lexical, scope. However, this temporary value is visible from everywhere, even if you call something in a different scope. The old, stashed away, value isn't accessable from Perlland.

And again, whether or not strict or warnings are enabled is utterly irrelevant.

As for your program, add the following, and see the difference:

print $::a, "\n"; # prints the empty string print $::b, "\n"; # prints 4


Comment on Re: my and local variable significance in a particular case
Select or Download Code
Re^2: my and local variable significance in a particular case
by Anonymous Monk on Apr 25, 2012 at 03:51 UTC

    Hello JavaFan, Thanks a lot for your reply. I just have one question , does $::a mean $main::a and similarly $::b equivalent to $main::b ? Thanks.

      Yes.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-09-20 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls