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

Ok, I did some reading about symbol tables, and if I understand them correctly, they are just hashes that contain the variables in your program. The keys in the hash are the variable's names and the values are the variable's values. However, the docs say that the symbol table hash does not contain "my" variables--only dynamic variables. What is a dynamic variable? Anything that isn't a my variable?

The place that I'd look for documentation on the symbol table is Symbol Tables, but I don't see any use of the term ‘dynamic variables’ there (and I wouldn't expect to see one in the Perl documentation; it's not a standard term). Did you look somewhere else?

Variables in Perl are either local *, in which case they are lexically scoped, or global, in which case they are dynamically scoped. It is global (or, better, ‘package’) variables that you will find in the stash. Lexicals are created with my, as you say; globals are created with our, or, if you are naughty and don't use strict, implicitly whenever you reference a previously un-declared variable.

UPDATE: Sorry, I was very very slow (had to go to class in the middle) and ikegami gave a (slightly more admonitory :-) ) reply first.

* Except that we call them lexicals, not locals, because they have nothing to do with the variables referenced (not created) by local.

Replies are listed 'Best First'.
Re^4: type glob
by 7stud (Deacon) on Nov 17, 2009 at 20:22 UTC

    The place that I'd look for documentation on the symbol table is Symbol Tables, but I don't see any use of the term ‘dynamic variables’ there (and I wouldn't expect to see one in the Perl documentation; it's not a standard term). Did you look somewhere else?

    I read the Symbol Table section of the docs in perlmod before posting, but I don't see how anyone would know after reading that section that my variables are not included in the symbol table. After I read that, I thought all variables in your program were included in the symbol table. It made sense to me that they would be.

    I got the term "dynamic variables" from the introductory paragraph higher up on the page:

    A package statement affects only dynamic variables--including those you've used local() on--but not lexical variables created with my().

    Reading the term 'dynamic variable' didn't make any sense to me.

      I thought all variables in your program were included in the symbol table. It made sense to me that they would be.

      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; }
        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.

      I read the Symbol Table section of the docs in perlmod before posting, but I don't see how anyone would know after reading that section that my variables are not included in the symbol table.
      I think it's a good point; I've read the linked passage again, and I also can't see how one would find out from that that only package variables are in stashes. The closest I can come in a few seconds' search is Symbolic references. In this case, I'd say that the way you know is that you ask a question like yours and someone tells you. :-)
      I got the term "dynamic variables" from the introductory paragraph higher up on the page
      Interesting! Well, I guess that there's a documentation patch waiting to be submitted.
      I read the Symbol Table section of the docs in perlmod before posting, but I don't see how anyone would know after reading that section that my variables are not included in the symbol table.

      Using quantifier like anyone is dangerous: no one knows how smart others are. :)

      For example, one may be guessing just the opposite: if the variable is "my", then it is not "others'", so it is probably not in package's symbols table for others to modify.