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

Re^6: typeglob/symbolic reference question

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


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

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.

Replies are listed 'Best First'.
Re^7: typeglob/symbolic reference question
by andal (Hermit) on Nov 08, 2010 at 10:19 UTC
    @{"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.

        I guess, I can't understand what is the meaning of "explanation for the syntax". I know that there's syntax for doing different things. I don't understand why the syntax has to be explained. I mean, it is not common to search for the explanation of syntax of English language. The syntax (together with words) makes English different from say Russian :)

        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.

        "strict refs" just tries to limit you to the rigid forms of "normal" usage. This is done in hope to catch some obscure errors. It does not change the fact, that *color and *{"color"} do the same thing. Though with the second syntax you can do more obscure things than with the first (that is why "strict" does not approve it :) ) Well, as long as your understanding does not limit you in what you can do with perl, there shouldn't be any problems.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-11-23 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?