Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

Replies are listed 'Best First'.
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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966753]
LanX wipes his face dry
NodeReaper passes a mop

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-03-21 13:44 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (267 votes). Check out past polls.