in reply to Re: Unconventional exit from while loop
in thread Unconventional exit from while loop

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.

Replies are listed 'Best First'.
Re^3: Unconventional exit from while loop
by LanX (Saint) on May 26, 2013 at 14:12 UTC
    > ... 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^3: Unconventional exit from while loop
by hdb (Monsignor) on May 26, 2013 at 14:17 UTC

    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.