in reply to How to exit subroutine and go to Beginning of CGI Script?

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.


Dave