http://qs321.pair.com?node_id=869421


in reply to Re^2: typeglob/symbolic reference question
in thread typeglob/symbolic reference question

I guess, your confusion comes from the presence of GLOB slot in the symbol table entry described in the book. In reality, the syntax *{"color"} references the symbol table entry as a whole, not the GLOB slot of it. When you assign some reference to this entry, then perl copies the reference into appropriate slot of the symbol table (type of reference defines which slot is taken).

The GLOB slot of symbol table entry mentioned in the perlref is something obscure. It is not reported by the Devel::Peek module. I guess, that this is just a syntax sugar. One can do either $a = \*other; or $a = *other{GLOB}; and both shall return the reference to the symbol entry as a whole. This reference can be used for reading files and doing something like *{$$a} = \$b; to alias $other with $b.

Reference to the symbol entry is not the same as an "alias" for the symbol entry. You can do $a = *other; here $a shall become alias of the symbol entry and you can say ${$a}[0] = 2; or ${$a}{key} = 3; or <$a> to read the file. All of this will actually use @other or %other etc. Effectively, it is an alternative to using references.

  • Comment on Re^3: typeglob/symbolic reference question

Replies are listed 'Best First'.
Re^4: typeglob/symbolic reference question
by 7stud (Deacon) on Nov 04, 2010 at 17:57 UTC
    In reality, the syntax *{"color"} references the symbol table entry as a whole, not the GLOB slot of it

    So if you write something like:

    *color = *other;

    does *other go in the GLOB slot?

    It sounds like what people are saying is that perl treats the syntax here:

     *{"color"} = ...

    differently than the rvalue syntax here:

    @arr = @{"color"};

    and in the first case the braces aren't dereferencing anything. So is the syntax:

     *{"color"} =

    just a way to use a string when constructing a typeglob's name? Somewhat like the braces in the following code also aren't dereferencing anything:

    my $color = "green"; print "${color}ery";
    When you assign a reference to a glob, it gets assigned to the appropriate slot. *foo = []; assigns to the ARRAY slot; *foo = sub {}; assigns to the CODE slot.

    Yes, I understand that aspect of typeglobs.

    What makes you think they might be equivalent?
    When I posted, I was not clear on what either syntax was doing. Now I understand that the braces in &{"color"} are an attempt to dereference a string, which means perl treats "color" as a "symbolic reference". To deal with a symbolic reference, perl must go to the symbol table and look up "color". Then the & preceding the braces tells perl to grab what's in the CODE slot for "color". As I understand things, that is in contrast to a hard reference, something like &{$some_coderef}, which directs perl straight to the address in memory where the subroutine is stored (bypassing the symbol table), and hence is more efficient.

    But I'm still unclear on how perl interprets *{"color"}.

      So if you write something like: *color = *other; does *other go in the GLOB slot?

      There's no "GLOB" slot really. When perl encounters such expression, it goes to symbols table and makes sure that "color" and "other" reference the same set of slots.

      It sounds like what people are saying is that perl treats the syntax here: *{"color"} = ... differently than the rvalue syntax here: @arr = @{"color"};

      Yes. But the difference is not between lvalue and rvalue usage. The difference is in the fact that '*' provides access to all of the slots and the '$', '&', '@' provide access to specific slot. Yet, the syntax *xxx{THING} again accesses specific slots. So, really, all you need to understand is the fact that '*' references all of the slots as the whole.

        The difference is in the fact that '*' provides access to all of the slots and the '$', '&', '@' provide access to specific slot.

        I understand "normal" typeglob syntax, i.e. that *color represents all variables with the name color. What I don't quite grasp is the syntax *{"color"}. Are the braces causing something to be dereferenced? Because as I understand it, when I write:

         @{"color"}

        perl interprets that as an attempt to dereference a string. So perl treats the string "color" as a symbolic reference, which causes perl to go to the symbol table and look up "color", and the @ tells perl to grab what's in the ARRAY slot for "color".

        Should I be interpreting:

        *{"color"}

        to mean: there is an attempt to dereference a string, so perl treats the string "color" as a symbolic reference and therefore perl looks up "color" in the symbol table. Then the * tells perl to create(?) a color typeglob? Or does the color typeglob already exist? I would like for the second choice to be true--for consistency, i.e. 'grab' something already there, rather than 'create' something.