Thanks for the replies
I don't know how your interpretation of & as the 'address of operator' makes sense in this dereferencing situation. When you dereference a reference you have to indicate what type you produce, and the '&' signifies a subroutine. For instance:
my $scalar_ref = \10;
my $x = ${$scalar_ref};
print "$x\n"; #10
my $arr_ref = [1,2,3];
my @arr = @{$arr_ref};
print "@arr\n"; #1 2 3
my $sub_ref = sub {print "hello\n"};
&{$sub_ref}();
In the first case, $ is used, then @, and finally & is used for the subroutine.
The syntax I am trying to understand is:
*{"color"} = ....
As I understand it, the braces are dereferencing a string, which means perl treats the string as a 'symbolic reference', which in turn causes perl to go to the symbol table and look for "color". Then I think the * must instruct perl to grab the glob slot that corresponds to color--at least that seems to be the way things work when a symbolic reference is on the righthand side:
#setup:
-------
$color = 10;
@color = (1,2,3);
sub color {print "hello\n"};
-------
$c = ${"color"}; #grab the scalar slot for 'color'
print "$c\n"; #10
@arr = @{"color"}; #grab the array slot for 'color'
print "@arr\n"; #1 2 3
&{"color"}(); #grab the subroutine slot for 'color'
#and execute the sub
Sorry if this appears to be a contrived example. It is a simplification of the code on p. 158 in Intermediate Perl that uses typeglobs and symbolic references to dynamically create subroutines.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|