Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: typeglob/symbolic reference question

by 7stud (Deacon)
on Nov 04, 2010 at 17:57 UTC ( #869529=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: typeglob/symbolic reference question
by andal (Hermit) on Nov 05, 2010 at 09:01 UTC
    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.

        @{"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".

        But if the name "color" does not exist in the symbol table, then perl will create this name. So, perl either creates new, or grabs existing. This is the consistent behavior of perl. So the *{"thing"} usage is as consistent as @{"thing"}. The only difference between the two is what is being accessed/created. Why do you need to put so many complications around it?

        #!/usr/bin/perl @{"color"} = qw(a b c); # this creates new name "color" # and uses ARRAY slot of the name @{"color-ab"} = qw(1 2 3); # this also creates new name # and uses ARRAY slot of it. print join(',', @color), "\n"; # here I use the name created above print join(',', @{"color-ab"}), "\n"; # I can't access this name # using "normal" perl syntax *{"other"} = *{"color-ab"}; # this creates new name "other", # and this name shall reference # the same slots as "color-ab" name print join(',', @other), "\n"; ${"color-ab"} = 10; # set "color-ab" variable print $other, "\n"; # see the difference in $other.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2020-10-29 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (274 votes). Check out past polls.

    Notices?