in reply to Re^6: type glob
in thread type glob

local doesn't create variables. It just backs up the value of the variable and restores it later. While it reduces the problems of using global variables, it doesn't remove them all.

Replies are listed 'Best First'.
Re^8: type glob
by JadeNB (Chaplain) on Nov 18, 2009 at 20:29 UTC
    local doesn't create variables. It just backs up the value of the variable and restores it later.

    I know *. Certainly there's an internal difference; but it seems to me that the casual user is not likely to encounter much difference between

    our $x; CODE INVOLVING OUTER $x { local $x; CODE INVOLVING INNER $x }
    and
    my $x; CODE INVOLVING OUTER $x { my $x; CODE INVOLVING INNER $x }
    That's by no means to say that there is no difference—indeed,
    our $x; my $y; sub twiddle { $x = $y = 'twiddled' } { local $x = 'local?'; my $y = 'local!'; twiddle; say $x, ', ', $y; }
    shows that there is—just that one's not likely to see it in casual use.

    * Ah, but re-reading shows that I didn't seem to understand: I said something about having two variables with the same name, when really the localised $x is the same variable as the original one, just with a ‘temporary’ value. Is that misstatement on my part what you were correcting?

      just that one's not likely to see it in casual use.

      If a child scope has a variable with the same name as its parent scope, it would look like

      my $x; sub foo { ... $x ... } sub bar { ... my $x ... }

      What you said boils down to saying bar is unlikely to call foo, and I don't see how that is true.

      I it is unlikely that the same var name is used at two levels in a working program, but that's something else.