in reply to difference between my and local

Imagine you have this:
# main scope $fred = 1; #global #... { # outer scope my $fred = 2; #... { #inner scope #... } }
In the outer scope, you could argue you don't need local because you can theoretically assign a localised $fred to the value of global $fred with just my $fred = $fred;. But in the inner scope, local provides the only means of access to global $fred (other than referencing it by package name) because otherwise access to global $fred is blocked by the presence of outer scope's $fred.

-M

Free your mind

Replies are listed 'Best First'.
Re^2: difference between my and local
by Eimi Metamorphoumai (Deacon) on Nov 15, 2005 at 14:10 UTC
    I'm not quite sure what you're getting at, but you can certainly get the global value from anywhere, without using local. Observe:
    # main scope $fred = 1; #global { # outer scope my $fred = 2; #... { #inner scope print $main::fred; # prints the global 1 } }
    The best rule of thumb is to use my unless you have some particular reason to use something else.
      You'll find that my is used more often than local. So what I am getting at is that whereas my $fred = $fred will have the same effect as local $fred most of the time, it won't always. I expect the OP to wonder why he ever needs local instead of just using my $fred = $fred rather than to wonder why he needs my - he already knows why he needs my and he already knows broadly what both of these do.

      -M

      Free your mind