in reply to shift in list context buggy?

The assignment operator will return a non-empty list: (undef) and therefore while will never stop.

use strict; use warnings; use Data::Dumper; my @a = ( 1, 2 ); my $x; my @b; while( @b = ( $x ) = shift @a ) { print Dumper \@b; }

Replies are listed 'Best First'.
Re^2: shift in list context buggy?
by LanX (Saint) on Nov 09, 2013 at 13:58 UTC
    Yes, of course¹, but the question was why !

    shift could easily return an empty list and $x is still undef.

    DB<178> $x=() => undef

    It's list context, no reason to act differently to splice.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ¹) tl;dr ?

      "shift could easily return an empty list and $x is still undef."

      It could easily, yes. But it doesn't. And it's documented as retuning undef. To do something different may well subtly break a lot of code on CPAN.

      That said, shift is one of those keywords that Perl allows you to override...

      use subs 'shift'; sub shift (+) { my $arr = $_[0]; @$arr ? CORE::shift(@$arr) : (); }
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        Just for curisosity - you write:
        shift is one of those keywords...
        The "sub"-perldoc uses similar vague language:
        Many built-in functions may be overridden
        Finally, some built-ins (e.g. "exists" or "grep") can't be overridden
        Is there a list somewhere of the built-ins that can and those that cannot be overridden or is "many" and "some" all that's documented?

      You have been talking about empty lists, so I thought I point out that there is no empty list involved.