Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: typeglob/symbolic reference question

by BrowserUk (Patriarch)
on Nov 04, 2010 at 00:43 UTC ( [id://869371]=note: print w/replies, xml ) Need Help??


in reply to typeglob/symbolic reference question

No. Your second example attempts to assign an anonymous subroutine to an lvalue sub called 'color'. If there is a sub called 'color' and it is not an lvalue sub you'll get:Can't modify non-lvalue subroutine call ... If it exists and is an lvalue sub, it would assign the address of the anonymous sub to its target lvalue.

This $x = 'color'; *{$x} = sub {#do something}; is equivalent to sub color { #do something };


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.

Replies are listed 'Best First'.
Re^2: typeglob/symbolic reference question
by 7stud (Deacon) on Nov 04, 2010 at 00:55 UTC

    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
    

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

      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.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found