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