Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Unconventional exit from while loop

by hdb (Monsignor)
on May 26, 2013 at 08:15 UTC ( [id://1035302]=note: print w/replies, xml ) Need Help??


in reply to Unconventional exit from while loop

Thanks all for your insightful replies. In summary I will probably:

  • not use last in this context anymore,
  • mark BrowserUK's version as the most maintainable one, that I really should be using,
  • often use tobyink's version, just to save one line of code,
  • and examine the iterator's interface carefully to make sure nothing unexpected can happen.
I also share Laurent_R's view that there should not be any taboos related to breaking out of loops as long as it helps to make the code clearer and supports the intention of the coder.

Replies are listed 'Best First'.
Re^2: Unconventional exit from while loop
by BrowserUk (Patriarch) on May 26, 2013 at 13:46 UTC
    not use last in this context anymore,

    Ever since I replied above, this has been ticking over in the back of my mind: I wasn't quite sure quite why I so prefer the normal version.

    Especially since I'm not adverse to using last in unusual ways.

    And I think finally the penny has dropped. It's because it effectively renders that while loop condition test redundant, but doesn't stop being tested each time around the loop. So you are effectively testing two conditions every time, only one of which can ever be false.

    Kind of like:

    while( 1 ) { my( $w, $x, $y, $z ) = @{ $iter->next // last }; ... }

    Except that the loop conditional isn't optimised away.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      This is what has caused me to ask the question in the first place. The condition in while always returns true until the iterator is exhausted, but before while has a chance to exit, last takes over. So the whole while statement seems redundant.

        So the whole while statement seems redundant.

        Yeah. This would probably be better:

        { my( ... ) = @{ $iter->next // last }; ... redo; }

        At least the exit and the cause of the exit is clear.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      > ... renders that while loop condition test redundant, ... only one of which can ever be false.

      Nope, the while loop condition exits on empty lists and your code doesn't do the same thing.

      Try returning an empty array ref [].

      see also Re: Unconventional exit from while loop

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        See

        "Kind of like"

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Unconventional exit from while loop
by LanX (Saint) on May 26, 2013 at 12:59 UTC
    I checked Algorithm::Combinatorics documentation and source, undef is exit-code and all other return values are array-refs.

    Note [] is listed as valid edge case but would make your loop exit.

    So the version from the synopsis (which is also BUK's) is the generic one for this module and works always...

    while (my $x = $iter->next() ){ ... }

    ...cause array-refs are always true.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-20 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found