in reply to Re: type glob
in thread type glob

Hi, thanks for the response.

I am trying to use a type glob to access values in a hash.

Why?

I read about them in the docs, and I wanted to try them out.

You're trying to reach a variable via a type glob in the symbol table, yet the variable in question isn't in the symbol table. It's a lexical (my) variable.

Hmm...things aren't as straight forward as I was led to believe. 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?

Well, if we are always told to use 'my' to declare variables, how does a program create "dynamic variables"?

I discovered this "solution" to my original question:

#use strict; #use warnings; use 5.010; %h = qw{a 1 b 2}; @h = (100, 200, 300); $h = 10; say $h{a}; say $h[0]; say $h; my $href = *h{HASH}; my %hash = %{$href}; say $hash{a}; --output:-- 1 100 10 1

I have a question about this code:

my %h1 = qw{a 1 b 2}; *h2 = \%h1;

How is that different from doing this:

my $href = \%h1;

Is h2 one of those dynamic variables?

say *h2; --output:-- main::h2

Hey, it's in the hash table. Oh boy, I'm on a roll:

say $main::h2{a}; --output:-- 1

Replies are listed 'Best First'.
Re^3: type glob
by ikegami (Patriarch) on Nov 17, 2009 at 19:20 UTC

    However, the docs say that the symbol table hash does not contain "my" variables--only dynamic variables.

    Let's forget that name and call them "package variables", please.

    What is a package variable? Anything that isn't a my variable?

    No, Perl also has anonymous variables. A package variable has the form

    $Foo::Bar::moo

    This can be abbreviated to

    $moo

    in some circumstances.

    Well, if we are always told to use 'my' to declare variables, how does a program create package variables?

    You seem to be missing the point. Don't use package variables. Always use lexical variables.

Re^3: type glob
by JadeNB (Chaplain) on Nov 17, 2009 at 19:46 UTC
    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.

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

        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.