Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^2: typeglob/symbolic reference question by 7stud
in thread typeglob/symbolic reference question by 7stud

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2021-10-27 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (93 votes). Check out past polls.

    Notices?