Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Calling subroutine with exec() in .cgi script

by morbid_ru (Initiate)
on Jan 18, 2006 at 22:53 UTC ( #524088=perlquestion: print w/replies, xml ) Need Help??

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


Here is my code:

exec ("/path/to/file/filename.cgi"); CORE::exit 1;

This works fine. However how can I call subroutine of filename.cgi?

In other words. With URL ...filename.cgi?action=task1 I call subroutine task1

How to do the same with exec()? This obviously gives an error:

exec ("/path/to/file/filename.cgi?action=task1");

Also, how can I path parameters to filename.cgi with exec()?

Such as I want to path $color=green

Thank you

Edited by planetscape - added code tags and formatting

Replies are listed 'Best First'.
Re: Calling subroutine with exec() in .cgi script
by Errto (Vicar) on Jan 18, 2006 at 23:00 UTC

    When you're calling a CGI script as an executable, you need to simulate the work that your web server would normally do for you. In particular, it means that you need to set up your environment to match the standard setup of a CGI program. At a minimum, you'll need to do $ENV{QUERY_STRING} = 'action=task1&color=green'; I have not tried this so I don't know what the minimal number of environment variables is to make something like respond properly, but I would guess that you need at least REQUEST_METHOD, QUERY_STRING, and SCRIPT_NAME.

    As an alternative, you can try taking the subroutines from filename.cgi that you need to reuse and stick them in a module somewhere that all your scripts can access. I suspect in the long term you'll find that an easier approach.

Re: Calling subroutine with exec() in .cgi script
by brian_d_foy (Abbot) on Jan 19, 2006 at 03:19 UTC

    What are you trying to do? One CGI script calling another through an exec() is pretty odd.

    Is this something that you can do with an internal redirect? The first script outputs a redirection header with a relative path. The server picks that up and handles the rest for you and it only looks like one request to the client.

    use CGI; my $cgi = CGI->new; $cgi->redirect( "/filename.cgi?action=task1" );
    brian d foy <>
    Subscribe to The Perl Review
Re: Calling subroutine with exec() in .cgi script
by graff (Chancellor) on Jan 19, 2006 at 03:46 UTC
    If the exec("/path/to/file.cgi") is meant to be called from a script that you run in a command-line shell, the easiest way to provide the cgi parameters to the cgi script that is being exec'd is to include them as a command-line arg, and in this case, it would be really important to use the "list" style invocation of exec():
    my $cgiprog = '/path/to/filename.cgi'; my $cgiparams = 'action=task1&user=me'; exec( $cgiprog, $cgiparams );
    Assuming that the cgi script being exec'd in this fashion is using the CGI module, $ARGV[0] will automatically be treated as the QUERY_STRING.
Re: Calling subroutine with exec() in .cgi script
by blazar (Canon) on Jan 19, 2006 at 09:28 UTC
    Calling subroutine with exec() in .cgi script

    exec, like the underlying system call, just substitutes the current process with another one: i.e. it executes another program and never returns. It is not for executing subroutines. (The most similar thing I can think of that is related to subroutines, is magical goto, but in "standard use" one rarely needs it and good old plain sub call is fine instead.)

Re: Calling subroutine with exec() in .cgi script
by kutsu (Priest) on Jan 19, 2006 at 16:10 UTC

    I second brian_d_foy that calling a second cgi script with exec is quite odd, and may require a redirect, but if depending on what the script does - might it be easier to turn it into a module? Simple Module Tutorial would be a good place to start if a module would work.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://524088]
Approved by Errto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2021-04-14 14:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found