Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: typeglob/symbolic reference question

by 7stud (Deacon)
on Nov 04, 2010 at 00:55 UTC ( [id://869372]=note: print w/replies, xml ) Need Help??


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

I have a follow up question: how do you interpret the left side of the following line:

*{"color"} = sub {#do something};

"Go to the symbol table and look up the name color and grab the glob slot"?

This is from Mastering Perl:

Package    Identifier           Type    Variable

                       +------> SCALAR - $bar
                       |
                       +------> ARRAY  - @bar
                       |
                       +------> HASH   - %bar
                       |
Foo:: -----> bar  -----+------> CODE   - &bar
                       |
                       +------> IO     - file and dir handle
                       |
                       +------> GLOB   - *bar
                       |
                       +------> FORMAT - format names
                       |
                       +------> NAME
                       |
                       +------> PACKAGE

Replies are listed 'Best First'.
Re^3: typeglob/symbolic reference question
by BrowserUk (Patriarch) on Nov 04, 2010 at 01:11 UTC

    Maybe this clarifies things?

    sub bill { say 'hi' };; bill;; hi *bill = sub { say 'bye' };; Subroutine main::bill redefined at (eval 8) line 1, <STDIN> line 3. bill;; bye $x = 'bill';; *{$x} = sub { say 'hi again' };; Subroutine main::bill redefined at (eval 11) line 1, <STDIN> line 6. bill;; hi again

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      And also
      *{$x}{CODE}->() hi again
      Interesting that assigning to *{$x}{CODE} is not allowed.
      *{$x}{CODE} = sub { print "hi" }; Can't modify glob elem in scalar assignment...
        Interesting that assigning to *{$x}{CODE} is not allowed.

        If I recall correctly--which i quite possibly don't--there is a proper syntax for doing that.

        But, like many people I suspect, I went through the process of playing with globs many moons ago, trying all the different forms and syntaxes, and then promptly forgot most of them because the are so few occasions when they are useful.

        It's one of those dark corners of Perl that's good to know it exists for the rare occasions that a use for it pops up. But when (if) it does, I have to go off and look up the syntax.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: typeglob/symbolic reference question
by ikegami (Patriarch) on Nov 04, 2010 at 02:39 UTC

    The symbol table entries are globs. They're associative arrays (like hashes) where the keys are data types. No, you're not grabbing what's in the glob slot of the glob, you're grabbing the glob itself.

    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.

Re^3: typeglob/symbolic reference question
by andal (Hermit) on Nov 04, 2010 at 09:31 UTC

    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://869372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-28 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found