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 (Saint) on Nov 09, 2013 at 14:39 UTC | |
by AnomalousMonk (Archbishop) on Nov 09, 2013 at 17:57 UTC |