in reply to Re^2: shift on empty array in list context broken
in thread shift on empty array in list context broken

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

use strict; use warnings; use feature qw'say state'; use diagnostics -verbose; #use Test::More test=>1; A B my @x @y Scalar Context my @x; my @y; if( ( fork() ) == 0 ){ if( ( fork() ) == 0 ){ say 'A: scalar context'; @x ? do{say 'A @x not empty'} : do{say 'A @x empty'}; my $s = shift @x; @x ? do{say 'A $s = shift @x; @x not empty'} : do{say 'A $s = shif +t @x; @x empty'}; defined $s ? do{say 'A defined $s'} : do{say 'A undefined $s'}; print "\n"; say 'A: scalar context'; @y ? do{say 'A @y not empty'} : do{say 'A @y empty'}; my $u = splice( @y,0,1 ); @y ? do{say 'A $u = splice @y; @y not empty'} : do{say 'A $u = spl +ice @y; @y empty'}; defined $u ? do{say 'A $u = splice @y; defined $u'} : do{say 'A $u + = splice @y; undefined $u'}; print "\n"; }else{ say 'B: scalar context'; undef @x; @x ? do{say 'B undef @x; @x not empty'} : do{say 'B undef @x; @x e +mpty'}; my $t = shift @x; @x ? do{say 'B $t = shift undef @x; @x not empty'} : do{say 'B $t += shift undef @x; @x empty'}; defined $t ? do{say 'B $t = shift undef @x; defined $t'} : do{say +'B $t = shift undef @x; undefined $t'}; print "\n"; say 'B: scalar context'; undef @y; @y ? do{say 'B undef @y; @y not empty'} : do{say 'B undef @y; @y e +mpty'}; my $v = splice( @y,0,1 ); @y ? do{say 'B $v = splice undef @y; @y not empty'} : do{say 'B $v + = splice undef @y; @y empty'}; defined $v ? do{say 'B $v = splice undef @y; defined $v'} : do{say + 'B $v = splice undef @y; undefined $v'}; print "\n"; exit; } say "A B Done\n"; exit; }else{ say "\n",'A B my @x @y Scalar Context',"\n"; }

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

use strict; use warnings; use feature qw'say state'; use Data::Dump; use diagnostics -verbose; # C D my (@x),(@y) List Context my (@x); my (@y); if( ( fork() ) == 0 ){ if( ( fork() ) == 0 ){ say 'C: list context'; @x ? do{say 'C @x not empty'} : do{say 'C @x empty'}; my ($s) = shift @x; @x ? do{say 'C ($s) = shift @x; @x not empty'} : do{say 'C ($s) = +shift @x; @x empty'}; defined $s ? do{say 'C defined $s'} : do{say 'C undefined $s'}; print "\n"; say 'C: list context'; @y ? do{say 'C @y not empty'} : do{say 'C @y empty'}; my ($u) = splice( @y,0,1 ); @y ? do{say 'C ($u) = splice @y; @y not empty'} : do{say 'C ($u) = + splice @y; @y empty'}; defined $u ? do{say 'C ($u) = splice @y; defined $u'} : do{say 'C +($u) = splice @y; undefined $u'}; print "\n"; }else{ say 'D: list context'; undef @x; @x ? do{say 'D undef @x; @x not empty'} : do{say 'D undef @x; @x e +mpty'}; my ($t) = shift @x; @x ? do{say 'D ($t) = shift undef @x; @x not empty'} : do{say 'D ( +$t) = shift undef @x; @x empty'}; defined $t ? do{say 'D ($t) = shift undef @x; defined $t'} : do{sa +y 'D ($t) = shift undef @x; undefined $t'}; print "\n"; say 'D: list context'; undef @y; @y ? do{say 'D undef @y; @y not empty'} : do{say 'D undef @y; @y e +mpty'}; my ($v) = splice( @y,0,1 ); @y ? do{say 'D ($v) = splice undef @y; @y not empty'} : do{say 'D +($v) = splice undef @y; @y empty'}; defined $v ? do{say 'D ($v) = splice undef @y; defined $v'} : do{s +ay 'D ($v) = splice undef @y; undefined $v'}; print "\n"; exit; } say "C D Done\n"; exit; }else{ say "\n",'C D my (@x),(@y) List Context',"\n"; }

Replies are listed 'Best First'.
Re^4: shift on empty array in list context broken
by LanX (Saint) on Jul 16, 2019 at 13:25 UTC
    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

      I am viewing the docs, x [maxdepth] expr\n\tEvaluates its expression in list context .... Would this affect the interpretation of the contexts?

        > Would this affect the interpretation of the contexts?

        No, the context of x is not propagated.

        the [...,...] is imposing list context

        DB<50> x [ shift @x, splice @x,0,1 ] 0 ARRAY(0x34a5270) 0 undef

        to enforce scalar context I used a scalar assignment

        DB<60> x [ ($a=shift @x), ($b=splice @x,0,1) ]

        if you have doubts enter a call to a sub which returns the context depending on wantarray

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