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

Re: shift in list context buggy? (scalar)

by tye (Sage)
on Nov 09, 2013 at 14:32 UTC ( [id://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 (Saint) 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.

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

    ²) best answer so far

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1061828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-03-19 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found