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.
| [reply] [d/l] [select] |
@{"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.
| [reply] [d/l] |
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.
| [reply] |