Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

exec a CGI from within another CGI

by belize (Deacon)
on Dec 07, 2000 at 02:08 UTC ( [id://45346]=perlquestion: print w/replies, xml ) Need Help??

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

I have a CGI that writes to a text file. After writing to the text file, I want another CGI to run and index that text file along with all the others in the directory.

The writing and indexing work fine. The problem comes with what is returned to the client. This is what I have:

print<<EOF text goes here EOF exec '/path/to/second/2.cgi' || $error($!); print<<EOF remaining text goes here EOF
The 2.cgi should output plain text saying what it did between the text outputted by 1.cgi. But either i only get what 2.cgi outputs, or a 500 error.

The output from 2.cgi is:

print "Content-type: text/plain\n\n"; print "Done-Captured $number-files total\n";
2.CGI works fine on its own, and with the content-type header, displays from 1.cgi, though the text from 1.cgi will not.

Hope this is clear.

Any ideas how to get output from both 1.cgi and 2.cgi to show?

Replies are listed 'Best First'.
Re: exec a CGI from within another CGI
by Fastolfe (Vicar) on Dec 07, 2000 at 02:47 UTC
    Note that exec, if successful, will terminate execution of the current program at that point, in favor of the new command that you're passing to exec. Thus, you will never see "remaining text goes here", because the exec succeeded.

    system functions like exec, but pauses until the other process is complete before returning control to your script. Likewise, qx// works like the one-argument form of system, but STDOUT is returned to your script in a string.

    Frequently when you end up having routines in one script that you need to utilize in another, it's useful to put the "common" routines in a module, and call that module from both of your scripts. This is a lot more efficient than spawning off new processes to do sub-tasks.

Re: exec a CGI from within another CGI
by arturo (Vicar) on Dec 07, 2000 at 02:11 UTC

    If they are both Perl scripts, why not incorporate 2.cgi as a subroutine within 1.cgi? 2.cgi could return whatever value is appropriate (i.e. the number of files it captures), and 1.cgi could print that out.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Thanks, exactly what I did. I had considered that, but then wanted to be able to run 2.cgi separately. Know what? I still can.

      Are thank you's worthwhile to post here? Or does it just take up space?

Re: exec a CGI from within another CGI
by swiftone (Curate) on Dec 07, 2000 at 02:22 UTC
    I agree with arturo, but I'll answer your question.

    You don't want exec. Try:

    $return_string = `/path/to/second/2.cgi`; #notice _back_ticks #You can check $return_string for error print $return_string;
    There are many threads on using exec() on this site, most of which will say to use backticks or qx().

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found