Berkeley CSUA MOTD:Entry 26881
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2002/12/21 [Computer/SW/Languages/Perl] UID:26881 Activity:nil
12/20   Perl question:
        # Doesn't work
        my @list = (1, 2, 3);
        my $name = "list";
        print("@$name\n");

        # Works
        @list2 = (4, 5, 6);
        $name2 = "list2";
        print("@$name2\n");
        \_ Short answer: replace the second line with  my $name = \@list;

           Long answer: using a string like "list" to access the variable
           @list is called a symbolic reference.  It works, but there are
           some restrictions -- for example, if the variable @list gets
           garbage-collected, your reference won't work anymore; also, as
           you noticed, it only works with global variables.

           If you use \@list instead of "list", then you get a real (non-
           symbolic) reference, which you can use exactly the same way but
           doesn't have the problems above.  See "man perlref" for details.
           --mconst
           \_ thx for the explanation.  I'm not sure my $name =\@list
              is what I want, because I may want to pass "$name" as a command
              line argument to choose between different lists.  Actually,
              someone may want to do that, not me.  I was just helping.