in reply to Re: arrays: shifting in while loop
in thread arrays: shifting in while loop

Thank you, you are correct! Re-examining my code in the interim between posting and your timely response, I noticed that I was in fact receiving a warning:
print (...) interpreted as function at - line 7.
Thank you for the clear explanation which has rectified my misunderstanding! I will also examine further your use of -MO=Deparse,p -e, which looks to be very useful indeed.

Replies are listed 'Best First'.
Re^3: arrays: shifting in while loop
by kcott (Archbishop) on Mar 06, 2012 at 15:09 UTC

    When you get a message like "print (...) interpreted as function ...", you can typically disambiguate your statement for Perl by simply adding a + in front of the opening parenthesis.

    Your original code runs like this:

    ken@ganymede: ~/local/bin $ perl -Mstrict -Mwarnings -E 'my $s = "foo bar baz qux 3 3 1 3"; my @ +a = split /\s/, $s; splice(@a, 0, 4); while (@a) { print (shift @a), +(shift @a), "\n"; }' print (...) interpreted as function at -e line 1. Useless use of a constant ( ) in void context at -e line 1. 31ken@ganymede: ~/local/bin $

    Adding a single + (changing ... print (... to ... print +(...), you get the output you were after:

    ken@ganymede: ~/local/bin $ perl -Mstrict -Mwarnings -E 'my $s = "foo bar baz qux 3 3 1 3"; my @ +a = split /\s/, $s; splice(@a, 0, 4); while (@a) { print +(shift @a), + (shift @a), "\n"; }' 33 13 ken@ganymede: ~/local/bin $

    This is discussed in more detail in perlop - Symbolic Unary Operators.

    -- Ken

Re^3: arrays: shifting in while loop
by Anonymous Monk on Mar 06, 2012 at 14:50 UTC
Re^3: arrays: shifting in while loop
by JavaFan (Canon) on Mar 06, 2012 at 15:07 UTC
    Do note you only get said warning because you use exactly one space between print and the parenthesis. Drop the space, use 2 spaces, or a tab, and the warning disappears. Replace print by warn, and the warning disappears as well.