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.