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


in reply to regexp list return 5.6 vs 5.8

i don't have access to 5.6 at the moment and so cannot investigate your question, but just as a matter of curiosity, why use the expression  return @rv[ 0 .. $#rv ]; when it seems to me it will return exactly the same thing as  return @rv; and when the latter expression does not involve the behavior of the  .. range operator in the case where the terminal value is less than the initial value, i.e.,  0 .. -1 in the case of  $#rv when the array  @rv is empty?

(you haven't been messing with  $[ have you?)

Replies are listed 'Best First'.
Re^2: regexp list return 5.6 vs 5.8
by Anonymous Monk on Jan 24, 2008 at 08:27 UTC
    hmmm...

    ok, but then wouldn't it be better (in the sense of being more maintainable) to write something like

        return wantarray ? @rv : $rv[-1];

    ?

Re^2: regexp list return 5.6 vs 5.8
by Sixtease (Friar) on Jan 24, 2008 at 07:50 UTC

    It's not quite the same ^^. @rv is an array and in scalar context evaluates to the number of the emelents. Slicing turns it into a list and thus gives the last element instead.

    use strict; use warnings; print "Just Another Perl Hacker\n";
      ETOOMUCHMAGIC. If that is what you want, state it:
      return wantarray ? @rv : pop @rv;

      Is the behavior of list slice subroutine return an intended feature, is it specced somewhere (or even documented)? Or is it just an implementation detail which might some day be considered a bug and be changed thereafter?

      Don't golf production code ;-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}