Re: How to exit subroutine and go to Beginning of CGI Script?
by hardburn (Abbot) on Dec 19, 2003 at 17:39 UTC
|
It can be done with goto, but it's a very, very bad idea. You probably need to rethink your design, but I couldn't say how without more details, so I leave the rest to you.
Update: After some CB chatter, some thought saying "very, very bad idea" was too strong without more explaination. However, I don't think I could do better than the classic Go To Statement Considered Harmful (external link).
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
: () { :|:& };:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by rchiav (Deacon) on Dec 19, 2003 at 17:48 UTC
|
if you haven't written anything out yet, you can use CGI::redirect() (or print a redirect header) to redirect the browser back to the script again. | [reply] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by duff (Parson) on Dec 19, 2003 at 17:38 UTC
|
One method is to make the "main" part of your code into a subroutine itself such that to start at the beginning again, you simply call that routine. I find your request quite bizarre though. Why would you want to do this?
| [reply] |
|
| [reply] |
|
It could lead to memory exhaustion. But then there's always goto &main; (or more likely @_=(); goto &main;). This form of goto isn't quite as evil as goto LABEL or goto EXPR.
P.S. Of course tail-recursion is possible, though not always desirable :)
| [reply] [d/l] [select] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by blue_cowdawg (Monsignor) on Dec 19, 2003 at 17:51 UTC
|
My first comment is that the way you are asking this
question leaves a ton of room for interpretation.
If your "subroutne" is in a seperate .pl file
how are you calling it? For sake of discussion I will
assume you are doing something like:
#!/usr/bin/perl -w
use CGI qw/ fatalsToBrowser/; # don't do this in production
use strict;
require "myfile.pl";
&foo(); # &foo is defined in myfile.pl
In that case if I want to start my script over again with
no regard to "state" or values of variables
then within foo() I would simply
.
.
. #much handwaving
exec($0);
.
.
.
Otherwise it gets slightly trickier. Check out the
CGI.pm documentation and look at the
redirect method. Yould do something like:
:
:
# NOTE: $q was defined elsewhere as my $q=new CGI;
print $q->redirect("http://my.site.tld/cgi-bin/foo.cgi?...")
:
:
:
Peter L. Berghold -- Unix Professional Peter at Berghold dot Net |
|
Dog trainer, dog agility exhibitor, brewer of
fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and
a good Belgian ale in your chalice. |
| [reply] [d/l] [select] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by demerphq (Chancellor) on Dec 19, 2003 at 17:56 UTC
|
exec $0;
Is one way. But its in the same class as goto, and like it is probably not a good idea on the whole. You should probably redesign your code so that this isnt needed. Perl has a whole host of looping constructs that can be used for this type of thing. Please have a review of perlsyn for more details on looping structures.
---
demerphq
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
| [reply] [d/l] [select] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by indigo (Scribe) on Dec 19, 2003 at 17:57 UTC
|
I can think of a few ways how you could do this, but I am kind of mystified as to why you would.
It seems to be rather a Bad Idea to me. For example, if you are just making a blind jump back to the beginning, how are you going to ensure you don't wind up back in the same subroutine and loop forever. Global variables? <shudder>
Anyway, maybe you could tell us a little more about what you are trying to accomplish, so we can help you find a Better Way.
| [reply] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by sauoq (Abbot) on Dec 19, 2003 at 18:30 UTC
|
Show us your code and explain the problem you are having. That will be more effective than asking us how to implement the solution you have in mind. Without the code, we have to guess anyway.
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by cees (Curate) on Dec 19, 2003 at 19:59 UTC
|
Like most of the comments above, I don't know exactly what you are trying to accomplish by doing this. But since you mention it is part of a CGI script, I am tempted to believe you are handling a POSTed form and after checking the parameters you would like to redisplay the original form with some error messages. But your program design is such that you need to start your program over to display the form again.
If this is the case, then I would suggest you look at some tools that help organize your code in a much more comprehensive way. Particularly have a look at the CGI::Application module. This lets you divide your CGI program up into "run modes", and it makes it trivial to jump from one "run mode" to another.
I would paste in an example, but the docs have lots of examples, and you can do a Super Search here at PerlMonks and find tonnes of help on using CGI::Application.
Good Luck
- Cees
| [reply] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by davido (Cardinal) on Dec 20, 2003 at 17:12 UTC
|
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.
| [reply] [d/l] [select] |
Re: How to exit subroutine and go to Beginning of CGI Script?
by TomDLux (Vicar) on Dec 19, 2003 at 19:52 UTC
|
I have a feeling you're taking a convoluted route to something that, with a different approach, can be handled by a loop, by exception handling, or by some other conventional programming methodology.
As everyone else says, show us your code, and explain what you're trying to achieve, rather than how you're trying to implement it.
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] |