Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: "last" not really outside loop block

by mscharrer (Hermit)
on Jun 22, 2009 at 15:50 UTC ( [id://773668]=note: print w/replies, xml ) Need Help??


in reply to "last" not really outside loop block

The tricky thing here is that do { ... } while (); is not a single syntax like in C. The do {} is one statement (and not a loop, which is a little confusing) and the postfix while just repeats this like any other statement.

Funnily, simple braces { } qualify as a 'loop' which is processed once. It can be aborted using last.

Therefore you could use:

$a = 3; { last unless $a == 4; redo; # if ( ... ); }
which works without an error message. But it isn't very good style. Try to use a normal while loop instead.

Supplement:
The perldoc entry for do also gives you the answer and a link to alternative strategies:

do BLOCK Not really a function. Returns the value of the last c +ommand in the sequence of commands indicated by BLOCK. When modified + by the "while" or "until" loop modifier, executes the BLOCK once before testing the loop condition. (On other state +ments the loop modifiers test the conditional first.) "do BLOCK" does not count as a loop, so the loop contro +l statements "next", "last", or "redo" cannot be used to leave or res +tart the block. See [doc://perlsyn] for alternative strategies.

Replies are listed 'Best First'.
Re^2: "last" not really outside loop block
by spx2 (Deacon) on Jun 22, 2009 at 15:55 UTC
    Try to use a normal while loop instead.

    I kind of need the post-condition and while only gives me pre-condition

      Try the follwing:

      Instead of:

      do { ... } while (expr);
      use:
      while (1) { ... last unless (expr); }

Log In?
Username:
Password:

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

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

    No recent polls found