in reply to Re: shift in list context buggy?
in thread shift in list context buggy?

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 ?

Replies are listed 'Best First'.
Re^3: shift in list context buggy?
by tobyink (Canon) on Nov 09, 2013 at 14:47 UTC

    "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?

        The Camel Book (4th edition, p. 411) is similarly vague:

        Many built-in functions may be overridden, although (like knocking holes in your walls) you should do this only occasionally and for good reason.

        But this 2001 node by danger:

        Re: Overloading Perl's Built-In's

        has a list of non-overridable built-in functions, together with the claim that these functions may be identified as “the ones defined to return a negative value in toke.c in the perl source code.” Alas, with my almost-zero knowledge of said perl source code, I can’t find any correlation between the given list and the contents of toke.c. :-(

        Anyway, hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        The first list in CORE gives you keywords that definitely cannot be overridden using use subs. I think perhaps that anything not listed there may be overridden, but I could be wrong.

        Generally the best way to find out if a particular function can be overridden is to try. And remember to test it on all the versions of Perl that you care about because this is something that has been changing quite a bit in recent releases - more and more keywords are becoming over-ridable.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        IIRC every built-in returning a valid prototype.

        Can't check ATM... Later more...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^3: shift in list context buggy?
by hdb (Monsignor) on Nov 09, 2013 at 15:12 UTC

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