Caviet lector: thought it's
possible to do something like this correctly, it's also
easy to do it wrong. If you're going to use a solution such as the one I'm about to provide, you need to be concerned with global variables that don't get properly reset to their original values, and filehandles that don't close.
That said, here's the simplest form I could come up with that has a prayer of maintaining decent lexical scoping:
MAIN: {
# All of your code goes here...
if ( $bad_scenario ) { redo MAIN; };
}
That's pretty simple, but you have to also worry about cleanup and housekeeping. If you need the ability to reset everything as a last ditch effort to restore your script to its original runstate, you might try this:
my $bad = 0;
MAIN: {
# Main code section.
print "Main block\n";
$bad = 1;
next MAIN if $bad;
last MAIN # This must be the last line of the main
# block to execute.
} continue {
# Close open filehandles, reset globals, etc.
print "Continue block\n";
if ( $bad ) {$bad=0; redo MAIN;};
It jumps around a bit, and isn't clean, but if done right, it could work.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.