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

Re^2: typeglob/symbolic reference question

by 7stud (Deacon)
on Nov 04, 2010 at 01:41 UTC ( #869382=note: print w/replies, xml ) Need Help??

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

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2021-10-27 14:05 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (93 votes). Check out past polls.