http://qs321.pair.com?node_id=869379


in reply to typeglob/symbolic reference question

unless absolutely necessary...mucking about with the symbolic table--just to implement a shortcut to a programming problem--is generally not a great thing. in this particular case i think a closure would be a cleaner solution. even if you're just learning about symbolic table by using some code..it's better to have example code that uses the symbolic table for good reason, or you could get into a habit of using symbolic table in a similar fashion to the artificial example provided. anyway, that's my rant about symbolic tables in perl.
i like to read the ampersand in similar way to reading C, where it's literally "address of" operator. it's not exactly that in perl, but probably close enough for most purposes. so in that context the example line starting with ampersand would read: the address of an anonymous sub containing code/literal "color" is assigned the sub on the right hand side.
but as per my rant...it would be less error prone and clearer to have a sub that takes the color as a parameter, and do something with that. with subs/functions things are easily extended..you get into a fight with symbolic table manipulations if you (for example) add similar code for handling (say) transparency and visibility etc other parameters. the main program starts looking like spaghetti code.
the hardest line to type correctly is: stty erase ^H
  • Comment on Re: typeglob/symbolic reference question

Replies are listed 'Best First'.
Re^2: typeglob/symbolic reference question
by ikegami (Pope) on Nov 04, 2010 at 02:51 UTC

    i like to read the ampersand in similar way to reading C, where it's literally "address of" operator.

    It's never anything like the "address of" operator in Perl. In fact, it's often used to do the opposite (dereference a code ref).

    so in that context the example line starting with ampersand would read: the address of an anonymous sub containing code/literal "color" is assigned the sub on the right hand side.

    Which goes to show your way to read the ampersand is wrong. Nothing of the kind happens.

      hi ikegami. dereferencing is literally accessing something at the address pointed by variable value. perl jargon is sigil...however perl does borrow many concepts (like referencing=~pointers nuts and bolts) from C. Maybe it's not the most useful concept (in my mindset) to bring across from C to perl with me...anyhow you can dereference a code reference in perl without the ampersand, so any argument about what ampersand actually does in perl is debatable, unless you actually see some requirement for it in compiled/assembled/running perl code. and it's not in current fashion for perl code afaik. maybe i should re-read the black book..but that's quite dated now.
      the hardest line to type correctly is: stty erase ^H

        Maybe it's not the most useful concept (in my mindset) to bring across from C to perl with me...

        Definitely, cause Perl's "&" never does anything similar to C's "&". Perl's "\" would be the closest to C's "&".

        anyhow you can dereference a code reference in perl without the ampersand

        $ref->(), although I don't see how the question relevant.

        so any argument about what ampersand actually does in perl is debatable

        Not really. It can be used to bypass prototypes (&func(...)), to call a func with parent's args (&func;), to dereference a code ref (&$ref) and surely more. None of them return an address (like C's "&") or anything similar (like Perl's "\").

        dereferencing is literally accessing something at the address pointed by variable value

        Not in any version of Perl. C pointers may be a fact of the implementation of Perl 5, but there is no language requirement for anything of the sort.

Re^2: typeglob/symbolic reference question
by 7stud (Deacon) on Nov 04, 2010 at 01:41 UTC

    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.