bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians:

The scenerio is simple (I think). I have several scripts, that when finished, need to start another common script, never to return. I prefer not to do a required or use.

I first tried

exec('newscript.pl');

but my screen just went blank. And no matter what I read about exec, I still think I might be misusing it.

Next I tried CGI's very own

print $query->redirect('http://www.domain.com/cgi-bin/newscript.pl');

and then

print "Location: http://www.domain.com/cgi-bin/newscript.pl\n\n";

both of which would have worked if I hadn't already printed a header earlier (a necessary cookie) giving me the Status: 302 Moved Location: error.

What are my options? Thanks!


—Brad
"Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

Replies are listed 'Best First'.
Re: How to run a Perl script from another script?
by graff (Chancellor) on Oct 05, 2004 at 01:23 UTC
    If "newscript.pl" is supposed to have access to the CGI parameters, this won't happen with just "exec('newscript.pl')" -- you would need to work out a way to convey the params safely via the command line that you pass to exec().

    But even if that isn't the issue, I still tend to agree with data64's opinion. You say you "prefer not to do a use or require", but you don't give any specific reason for that. In the absence of any specific reason, I'd say "get over it", and turn "newscript" into a module that your various other scripts can "use".

      graff, I appreciate your candor. The reason is that new script is really the more weighty script, basically a full-fledged "program" that I have a hard time thinking of as a merely module. There truly is a transfer of purpose and direction when leaving the one earlier script and going to the new script.

      Hope that helps.


      —Brad
      "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
        ... that new script is really the more weighty script, basically a full-fledged "program" that I have a hard time thinking of as a merely module.

        Do you mean that sometimes this new script is run entirely by itself, without any prior script being run ahead of it? I wasn't expecting this... Based on the initial question, I assumed that you have several scripts that each produce some distinct beginning portion of a page, and the "newscript" provides a common final portion for all those different pages. If that's what you're doing, then it still seems more sensible as a module, regardless of its size.

        (On the other hand, if the script really does work on its own without any prior script ahead of it, I'm a little unclear about how you are adapting it to this "secondary" role.)

        Do you have some different reason for "chaining" scripts in this way?

        Turning a script into a module doesn't need to involve a lot of fuss. Just put in a "package" statement at the top and a "1;" at the end, and instead of printing to STDOUT, have it accumulate text into a return string. Maybe you'll need a little bit of "reorganization" to have a single sub that returns the string (by doing whatever "main" does in the current version).

Re: How to run a Perl script from another script?
by JediWizard (Deacon) on Oct 05, 2004 at 02:07 UTC

    I am a little unclear on what exactly you want here. You speak of executing a script form another script, but you also make reference to "redirecting" to a script from another CGI. If you want to redirect to one CGI from another, and the only limiting factor is that the first CGI needs to set a cookie, a single header can both set a cookie and perform a redirect. If there is some significant reason that you must first print the header (containing the cookie) and later do a redirect, a simple javascript window.location = 'url_for_new_cgi'; could serve your purpose.

    May the Force be with you
Re: How to run a Perl script from another script?
by data64 (Chaplain) on Oct 05, 2004 at 01:03 UTC

    You could use do.

    However, I personally prefer to convert the other scripts into modules and then use them.


    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

Re: How to run a Perl script from another script?
by sgifford (Prior) on Oct 05, 2004 at 04:29 UTC
    exec sounds like it should do what you want. Have you tried checking the error status to see if the exec is failing?
    exec('newscript.pl') or die "Couldn't exec newscript.pl: $!";

    If not, what do you mean by my screen just went blank? What exactly happened?

Re: How to run a Perl script from another script?
by bwelch (Curate) on Oct 05, 2004 at 14:28 UTC
    When calling scripts or executing shell commands, I prefer to use this type of command so a return code is captured.

    my $jobStatus = `bjobs $jobID`;

    Are there any reasons to avoid this method? Do you prefer other methods for capturing the return code?