in reply to Do/while correct answer loop

I tend to avoid the do {...} while $cond loop because it's not a real loop. You can't use next/last/redo with it. As I said in this week's poll, I use a naked block with last and redo:
{ stuff more stuff last if $cond; # middle exit still more stuff again more stuff redo if $cond; # repeat as long as $cond is true }
This way you can name the loop (for multiple break-outs). If you want next OUTERLOOP to work, you merely next to add additional continue block:
OUTERLOOP: { blah while ($foo) { blah next OUTERLOOP if $cond; blah } blah } continue { redo if $cond; }
Naked blocks are fun!

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: RE: Do/while correct answer loop
by turnstep (Parson) on May 12, 2000 at 19:54 UTC

    Yes, but when else am I going to use poor old "do/while?" :)

    I admit, the bare block does look better:

    { print "\nDo you want to <P>lay again or <Q>uit?\n\n"; ($answer=<STDIN>) =~ /^[PQ]/i or redo; }