Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: referencing list

by sauoq (Abbot)
on Jun 06, 2012 at 01:12 UTC ( [id://974606]=note: print w/replies, xml ) Need Help??


in reply to referencing list

$ perl -le 'my @x=(1,2,3); (*a,*b,*c) = \(@x); $a="f"; $b="o"; $c="o"; + print @x' foo
-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: referencing list
by pmilne (Novice) on Jun 06, 2012 at 03:38 UTC
    Thanks, this seems to be what I am looking for. However if I add 'use strict;', I get the message 'Global symbol "$c" requires explicit package name ...'.
      However if I add 'use strict;', I get the message 'Global symbol "$c" requires explicit package name ...'.

      Yes, and if you named $a and $b something more reasonable, you'd hear about them too. (You only don't because they are special case variables used in sort blocks.) You can take care of that by declaring your globals though, and that's something you should be doing anyway. The following is safe under warnings and strict.

      use warnings; use strict; my @x = (1,2,3); our ($a,$b,$c); (*a,*b,*c) = \(@x); $a = "f"; $b = "o"; $c = "o"; print sort {$b<=>$a} qw(17 11 13 29 31 37 3 2 7 5 19 23); print @x;
      It also shows that using $a and $b is fine, despite also using them for a sort block. The problem with using $a and $b comes up when you have them declared as lexicals...
      $ perl -le 'my $a=1; print sort {$b<=>$a} 1' Can't use "my $a" in sort comparison at -e line 1.
      Since we all usually declare our variables as lexicals these days, the advice "don't use $a and $b" get dispensed without a lot of understanding about why it matters. Or when it doesn't.

      Update: By the way, Data::Alias is a fine module and you shouldn't hesitate to use it if you can and it passes its tests. Sometimes people don't have control over what perl modules are available in their environment, particularly ones with an XS component. And sometimes modules have bugs, particularly platform specific ones, that prevent them being used. And sometimes adding a module isn't worth it when a couple extra lines of code will do. This is why I offered the old way of doing this. It's up to you to decide what is most appropriate in your situation.

      -sauoq
      "My two cents aren't worth a dime.";

      imho, using symbolic refs is not advisable in production code for something this simplistic.

      If you know when and why to use them, they are great, but they are generally reserved for very advanced uses. However, I really do like sauoq's take on the problem though ;)

      Update: yes... under strict, their code will break. Kudos for having strict enabled to begin with.

        using symbolic refs is not advisable in production code for something this simplistic.

        These are not symbolic refs. These are aliases. There are good reasons to use aliases. Symbolic references are usually best avoided. This is an example of a symbolic ref:

        perl -le '$variable = 42; my $symref = "variable"; print $$symref;' 42

        -sauoq
        "My two cents aren't worth a dime.";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found