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

Then you couldn't have multiple variables with the same name, as in:
my $x; sub foo { my $x; my $x; { my $x; } } sub bar { my $x; }

It seems to me that your code behaves almost exactly like

our $x; sub foo { local $x; local $x; { local $x; } } sub bar { local $x; }
(except that localising a variable twice doesn't warn, whereas mying twice does) *. Thus, I'm not sure that it's accurate to say that you can't have two variables, both with the same name, in the symbol table **. (You might respond that you can only access one of them at a time, but that's true for lexicals, too.)

* Of course, since neither code does anything, this might be an even more vacuous statement than is usual for me. :-)
** Ah, but the $x of local $x is the same variable as that of our $x, whereas the $x of my $x is not the same variable as that of the other my $x.

Replies are listed 'Best First'.
Re^7: type glob
by ikegami (Patriarch) on Nov 18, 2009 at 19:03 UTC
    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.
      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.