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
In reply to Re: shift in list context buggy? (scalar)
by tye
in thread shift in list context buggy?
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |