Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: typeglob/symbolic reference question

by andal (Hermit)
on Nov 05, 2010 at 09:01 UTC ( #869638=note: print w/replies, xml ) Need Help??


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

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.

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

Replies are listed 'Best First'.
Re^6: typeglob/symbolic reference question
by 7stud (Deacon) on Nov 05, 2010 at 17:39 UTC

    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.

        Why do you need to put so many complications around it?

        Sorry. I just wanted to find an explanation for the syntax. I understand what the net result is--but if I don't understand how perl interprets the syntax then I won't be able to figure out what is happening in other contexts.

        I guess for now I'll just have to go with *color and *{"color"} are equivalent--even though *{"color"} is not legal under strict refs.

        Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-11-01 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (291 votes). Check out past polls.

    Notices?