Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Case where '( shift @_ )[ 0, 0 ]' returns only one value?

by rsFalse (Hermit)
on Jan 14, 2019 at 20:58 UTC ( #1228545=perlquestion: print w/replies, xml ) Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
These two looks similar to me, but they behave different:
perl -le '@_ = qw( a b c ); print( ( shift @_ )[ 0, 0 ] );'
OUTPUT: aa
perl -le '@_ = qw( a b c ); @A = ( shift @_ )[ 0, 0 ]; print @A'
OUTPUT: a
Seems like a bug?
My overcome was trivial:
perl -le '@_ = qw( a b c ); @A = ( shift @_ ) x 2; print @A'

Update. This behaviour spans some versions, as shown by stevieb in his testing: Re^4: Case where '( shift @_ )[ 0, 0 ]' returns only one value?.
Behaviour (bug) fixed between 5.18 and 5.20, more details --> https://rt.perl.org/Public/Bug/Display.html?id=133769#txn-1606556

Replies are listed 'Best First'.
Re: Case where '( shift @_ )[ 0, 0 ]' returns only one value?
by AnomalousMonk (Bishop) on Jan 14, 2019 at 22:30 UTC

    A possibly helpful test:

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; note 'perl version: ', $]; ;; @_ = qw(a b c); note 'raw print list context shift: ', (shift @_)[ 0, 0, 0 ]; ;; @_ = qw(a b c); ok 'aaa' eq join('', (shift @_)[ 0, 0, 0 ]), 'joined shift'; ;; @_ = qw(a b c); my @ra = (shift @_)[ 0, 0, 0 ]; is_deeply \@ra, [ 'a', 'a', 'a' ], 'assigned shift'; ;; @ra = ('x', 'y', 'z')[ 0, 0, 0 ]; is_deeply \@ra, [ 'x', 'x', 'x' ], 'assigned list'; ;; done_testing; " # perl version: 5.014004 # raw print list context shift: aaa ok 1 - joined shift not ok 2 - assigned shift # Failed test 'assigned shift' # at -e line 1. # Structures begin differing at: # $got->[1] = undef # $expected->[1] = 'a' ok 3 - assigned list 1..3 ok 4 - no warnings 1..4 # Looks like you failed 1 test of 4.

    Update: The problem seems to have been localized between about versions 5.14.x and 5.18.x inclusive so the following may be irrelevant, but just for the heck of it, the code above passes all tests for ActiveState 5.008009 and Strawberries 5.010001 and 5.012003, all running under Windows 7.


    Give a man a fish:  <%-{-{-{-<

Re: Case where '( shift @_ )[ 0, 0 ]' returns only one value?
by rsFalse (Hermit) on Jan 14, 2019 at 22:27 UTC
    I sent a perlbug message with a link to this node.(It hasn't appeared on a list yet. After it appears i'll leave a link here).

    UPD. Here - https://rt.perl.org/Public/Bug/Display.html?id=133769 (Have sent it by email with attached file, idk how I failed to do that by perlbug+sendmail previously :/ ).
      > I sent a perlbug message with a link to this node.(

      Please correct me, but as far as I understood this thread, does the bug not appear in newer Perl versions.

      Do you expect patches for older versions?

      Or is it just for documentation?

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

        Just for documentation

      Nice work. This is why Perl is by far the best language around; teamwork and dedication.

Re: Case where '( shift @_ )[ 0, 0 ]' returns only one value?
by AnomalousMonk (Bishop) on Jan 15, 2019 at 01:43 UTC

    Another oddity: the problematic behavior seems to be associated with assignment to a freshly declared lexical:

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; note 'perl version: ', $]; ;; @_ = qw(a b c d e f g); ;; note 'assign to a fresh lexical'; ;; my @ra = (shift @_)[ 0, 0, 0 ]; is_deeply \@ra, [ 'a', 'a', 'a' ], '1st = (shift)[slice] to my @ra'; ;; @ra = (shift @_)[ 0, 0, 0 ]; is_deeply \@ra, [ 'b', 'b', 'b' ], '2nd = (shift)[slice] to @ra'; ;; note 'now another lexical'; ;; my @rb = (shift @_)[ 0, 0, 0 ]; is_deeply \@rb, [ 'c', 'c', 'c' ], '1st = (shift)[slice] to my @rb'; ;; @rb = (shift @_)[ 0, 0, 0 ]; is_deeply \@rb, [ 'd', 'd', 'd' ], '2nd = (shift)[slice] to @rb'; ;; @rb = (shift @_)[ 0, 0, 0 ]; is_deeply \@rb, [ 'e', 'e', 'e' ], '3rd = (shift)[slice] to @rb'; ;; done_testing; " # perl version: 5.014004 # assign to a fresh lexical not ok 1 - 1st = (shift)[slice] to my @ra # Failed test '1st = (shift)[slice] to my @ra' # at -e line 1. # Structures begin differing at: # $got->[1] = undef # $expected->[1] = 'a' ok 2 - 2nd = (shift)[slice] to @ra # now another lexical not ok 3 - 1st = (shift)[slice] to my @rb # Failed test '1st = (shift)[slice] to my @rb' # at -e line 1. # Structures begin differing at: # $got->[1] = undef # $expected->[1] = 'c' ok 4 - 2nd = (shift)[slice] to @rb ok 5 - 3rd = (shift)[slice] to @rb 1..5 ok 6 - no warnings 1..6 # Looks like you failed 2 tests of 6.
    (These tests all pass for all other versions I have in captivity; see update to this reply.)


    Give a man a fish:  <%-{-{-{-<

Re: Case where '( shift @_ )[ 0, 0 ]' returns only one value?
by stevieb (Canon) on Jan 14, 2019 at 21:19 UTC

    All three produce aa on my Perl 5.26.1 running on my Linux Mint workstation. What version of Perl are you running yours under?

    spek@scelia ~/scratch $ perl -le '@_ = qw( a b c ); print( ( shift @_ +)[ 0, 0 ] );' aa spek@scelia ~/scratch $ perl -le '@_ = qw( a b c ); @A = ( shift @_ )[ + 0, 0 ]; print @A' aa spek@scelia ~/scratch $ perl -le '@_ = qw( a b c ); @A = ( shift @_ ) +x 2; print @A' aa spek@scelia ~/scratch $ perl -v This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-li +nux
      Looks like some kind of change somewhere between 5.18.4 and 5.20.3:
      [~]$ perlbrew exec perl -le '@_ = qw( a b c ); @A = ( shift @_ )[ 0, 0 + ]; print @A' [...] perl-5.22.2 ========== aa perl-5.20.3 ========== aa perl-5.18.4 ========== a perl-5.16.3 ========== a perl-5.14.4 ========== a

        I confirm these findings (on Perl running on Windows at least).

        > berrybrew exec perl -le "@_ = qw( a b c ); @A = ( shift @_ )[ 0, 0 ] +; print @A" perl-5.28.1_64 ============== aa perl-5.20.3_64 ============== aa perl-5.18.4_64 ============== a
      This is perl 5, version 18, subversion 2 (v5.18.2)

        Ok. I'll do some installs of other versions on Linux and Windows and try to get to the version where it's borked while I finish my work day. On my Windows system I sometimes develop on, with 5.28.1, I get the aa as well:

        c:\repos\berrybrew>perl -le "@_ = qw( a b c ); print( ( shift @_ )[ 0, + 0 ] );" aa c:\repos\berrybrew>perl -le "@_ = qw( a b c ); @A = ( shift @_ )[ 0, 0 + ]; print @A" aa c:\repos\berrybrew>perl -v This is perl 5, version 28, subversion 1 (v5.28.1) built for MSWin32-x +64-multi-thread

        I got the same thing with perl 5, version 18, subversion 1 (v5.18.1) built for x86_64-linux-thread-multi

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1228545]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2020-01-28 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?