Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: shift in list context buggy? (scalar)

by tye (Cardinal)
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        


Comment on Re: shift in list context buggy? (scalar)
Select or Download Code
Re^2: shift in list context buggy? (best answer so far!)
by LanX (Canon) 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
Node Status?
node history
Node Type: note [id://1061828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-08-20 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (117 votes), past polls