P is for Practical PerlMonks

### Re: shift in list context buggy? (scalar)

by tye (Sage)
 on Nov 09, 2013 at 14:32 UTC ( #1061828=note: print w/replies, xml ) Need Help??

in reply to shift in list context buggy?

```my( \$a, \$b ) = ( shift @as, shift @bs );

There are lots of constructs where returning an empty list can be surprising if one expects a scalar to be returned.

splice returns a list and so should certainly return an empty list for edge/failure cases. Going back in time, I could see defining shift as returning either 1 scalar or returning the empty list, making that expectation clear, and that decision being a fine one. But I also can see sanity in defining shift as returning a scalar (the current reality).

Consider the relative difficulty of fixing your problem case vs. my counter-factual problem case:

```while( (\$x) = shift @a ) {    # Broken
while(  \$x  = shift @a ) {    # Fixed
while( @a ) {
\$x = shift @a;            # Better

my( \$a, \$b ) = ( shift @as, shift @bs );
my( \$a, \$b ) = ( scalar shift @as, scalar shift @bs );

Also, consider how likely one is to notice the breakage between the two scenarios.

This leads me to currently slightly prefer the definition of shift as "returns a scalar", that is, the status quo.

- tye

Replies are listed 'Best First'.
Re^2: shift in list context buggy? (best answer so far!)
by LanX (Chancellor) on Nov 09, 2013 at 14:39 UTC
Thx!²

But

> Consider the relative difficulty of fixing your problem case vs. my counter-factual problem case:

while(  \$x  = shift @a ) {    # Fixed

this is not a fix because while stops, if any element of @a is false.

```  DB<187> @a=(1,undef,2,3,0,4,5)

DB<188> print \$x while \$x =shift @a
1
DB<189> print \$x while \$x =shift @a
23
DB<190> print \$x while \$x =shift @a
45
DB<191> print \$x while \$x =shift @a

Thats the old semipredicate problem, which can only be solved with list-assignments.¹

But I agree with you that it's most probably too late to fix that design decision...

Cheers Rolf

( addicted to the Perl Programming Language)

¹) for completeness: no defined doesn't help here, if undef is a legal value.

• corrected "semipredicate problem"
• improved code example
• your "better" update is indeed better

But I agree with you that it's most probably too late to fix that design decision...

Amen to that, brother. Trying to fix | change this ancient design decision now would provoke sustained and piercing shrieks of anguish that would make the howls induced by the recent hash ordering 'fix' sound by comparison as mellow as a Hash Bash gathering!

Create A New User
Node Status?
node history
Node Type: note [id://1061828]
help
Chatterbox?
 [karlgoethebier]: shmem: Yes. Greetings from Dr. Freud [Discipulus]: blue pain is a synōnymía [karlgoethebier]: Discipulus: Just a typo in this case ;-)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-05-24 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (183 votes). Check out past polls.