Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Difference b/w my and local var?

by boom (Scribe)
on Mar 11, 2009 at 13:40 UTC ( [id://749876]=note: print w/replies, xml ) Need Help??


in reply to Difference b/w my and local var?

The variables declared with my() are visible only within the scope of the block which names them.

They are not visible outside of this block, not even in routines or blocks that it calls.

local() variables, on the other hand, are visible to routines that are called from the block where they are declared.

Neither is visible after the end of the block at all.

Replies are listed 'Best First'.
Re^2: Difference b/w my and local var?
by ikegami (Patriarch) on Mar 11, 2009 at 13:51 UTC

    local() variables, on the other hand, are visible to routines that are called from the block where they are declared.

    Not quite. It doesn't make the variables visible outside the block.

    sub foo { ??? how do you access the var ??? } { my %hash; local $hash{foo} = 123; foo(); }

    It doesn't even create variables as your wording suggests. It's not that what you said is wrong, just unclear and/or misleading. (Compare to what I used earlier.)

      Of course if you resort to minor evil there is a way to get at it . . . :)

      use PadWalker qw( peek_my ); sub foo { my $ebil = peek_my( 1 ); print $ebil->{'%hash'}->{foo}, "\n"; } { my %hash; local $hash{foo} = 123; foo(); }

      (Not that that's germane to the correct point you were making; just an interesting aside your ???s brought up :)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-23 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found