in reply to shift on empty array in list context broken

shift may not be the culprit, if you look at how the assignment is evaluated. Both the scalar and array assignment are evaluated in scalar context

DB<41> x { $max=100; while ( () = undef ) {print "$x, "; last if $ +max-- == 0 } } , , , , , , , , , , , , , , , ... , , , , , , , , , , , , , , , , , , +, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , + , , , , , , , , , , , empty array DB<42> x { $max=100; while ( $_ = undef ) {print "$x, "; last if $ma +x-- == 0 } } 0 undef

I dont know if this might be affecting how shift is working, but may shed some light.

I did investigate the splice/shift difference a little, they behave the same when both @x and @array are undef, but different when, as you highlighted they are empty.

Possibly shift and splice are optimised at that level due to their expected usage.

Update: this may be something to consider.

DB<60> x { $max=100; while ( @x = splice( @{[]},0,1 )) {print "$x, " +; last if $max-- == 0 } } 0 0 DB<61> x { $max=100; while ( @x = shift @{[]} ) {print "$x, "; last +if $max-- == 0 } } , , , , , , , , , , , , , , ... , , , , , , , , , , , , , , , , , , , +, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , + , , , , , , , empty array

Replies are listed 'Best First'.
Re^2: shift on empty array in list context broken
by LanX (Saint) on Jul 16, 2019 at 02:07 UTC
    > they behave the same when both @x and @array are undef,

    not sure what you mean here...(?)

    anyway I doubt it's possible to "fix" this, much code will rely on shift returning undef.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I'm not exactly sure either. I attempted to write some Test scripts. Getting only as far as comparing a couple of variations.

      heres two scripts that start out looking to compare list and scalar context asignment. I'm not sure they have picked up what you are viewing, but could possibly get there?

      A B my @x @y Scalar Context

      C D my (@x),(@y) List Context

        well not really short and concise...

        undef is/was IIRC meant to free memory.

        though I can't reproduce any difference between an emptied and destroyed array.

        list context different:

        DB<49> undef @x DB<50> x [ shift @x, splice @x,0,1 ] 0 ARRAY(0x34a5270) 0 undef DB<51> @x=() DB<52> x [ shift @x, splice @x,0,1 ] 0 ARRAY(0x34a6c48) 0 undef DB<53>

        scalar context same

        DB<54> undef @x DB<55> x [ ($a=shift @x), ($b=splice @x,0,1) ] 0 ARRAY(0x34a6960) 0 undef 1 undef DB<56> @x=() DB<57> x [ ($a=shift @x), ($b=splice @x,0,1) ] 0 ARRAY(0x34a6f18) 0 undef 1 undef

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice