in reply to Pulling out an element from a function return without using an intermediate list

I always do
my (undef, $param, undef, $other, @stuff) = thingy();
Doesn't seem very ugly to me. At least not as ugly as the other options. Your taste may vary :-)

  • Comment on Re: Pulling out an element from a function return without using an intermediate list
  • Download Code

Replies are listed 'Best First'.
Re^2: Pulling out an element from a function return without using an intermediate list
by throop (Chaplain) on Feb 19, 2007 at 22:11 UTC
    Really?
    use strict; my(undef, $aa, $bb) = dir(); Can't declare undef operator in my at (eval 159) line 2, near ") ="
      use strict; use warnings; my (undef, $param, undef, $other, @stuff) = dir(); print "undef, $param, undef, $other, @stuff"; sub dir { return (1, 2, 3, 4, 5, 6); }

      Prints:

      undef, 2, undef, 4, 5 6

      Works fine for me. What version of Perl are you using?


      DWIM is Perl's answer to Gödel

      On the other hand, the following versions all work:

      my $aa; my $bb; (undef, $aa, $bb) = dir();
      my ($aa, $bb); (undef, $aa, $bb) = dir();
      (undef, my $aa, my $bb) = dir();
      (undef, my ($aa, $bb)) = dir();
      my ($aa, $bb) = ( dir() )[1, 2];

      Update: Apparently, so does the following (contrary to the parent's claims):

      my (undef, $aa, $bb) = dir();

      Tested the last one on Perl 5.6.0, 5.6.1, 5.8.0 and 5.8.8.