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

CGI.pm usually lets me pass parameters to a Perl CGI script, when the script is executed from the command line. However, I can't pass the parameters to a Perl CGI script, when the script is executed within the Perl system function. The following CGI script (called test7.pl) reads the query string and prints it to standard error.
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; my $x = query_string() || 'undefined'; warn("x =$x"); print("Hello\n"); #print param('name')."\n";
The next CGI script (called test8.pl) uses the system function to pass data to test7.pl
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; print header(), start_html('test'); print h1(system("./test7.pl name=5 val=3 x=13 > temp.dat; mv temp.dat +temp1.dat")); print h1(system("./test7.pl name=5 val=4")); print end_html();
If I run test8.pl from the shell, we see the passed parameters.
x =name=5;val=3;x=13 at ./test7.pl line 8. x =name=5;val=4 at ./test7.pl line 8.
If I run test8.pl from the browser, we don't see the parameters.
sh: line 1: temp.dat: Permission denied mv: temp.dat: No such file or directory x =undefined at ./test7.pl line 8.
Why doesn't the system function pass the CGI parameters in? And why does the redirection fail?

Replies are listed 'Best First'.
Re: Why doesn't the system function pass the CGI parameters in?
by jasonk (Parson) on Dec 29, 2004 at 22:37 UTC

    The arguments don't get passed because CGI.pm only reads the command line arguments if $ENV{REQUEST_METHOD} is not set to GET, POST, or HEAD.

    The redirection fails because the user the web server runs as doesn't have permission to write to the directory you are trying to redirect into (hence the abundantly clear error message 'temp.dat: Permission denied' which is helpfully provided.

    You can work around the argument passing part by doing this in test8.pl:

    print header(), start_html('test'); { local $ENV{REQUEST_METHOD}; print h1(system("./test7.pl name=5 val=4")); } print end_html();

    As for the second part, you either need to make the directory writable by the web server user, or redirect into another directory (such as /tmp) which is writable.


    We're not surrounded, we're in a target-rich environment!
Re: Why doesn't the system function pass the CGI parameters in?
by dragonchild (Archbishop) on Dec 29, 2004 at 20:23 UTC
    print h1(system("./test7.pl name=5 val=3 x=13 > temp.dat; mv temp.dat +temp1.dat"));

    You sure that "./test7.pl" will work given your webserver's default directory? I'm betting that it won't ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Why doesn't the system function pass the CGI parameters in?
by Rocinante (Initiate) on Dec 29, 2004 at 21:04 UTC
    I have seen this behavior as well, using Apache 2.0.51(?) on Win2k.

    I never did solve the underlying problem - I finally ended up reading in the file I wanted to execute, adding a few lines at the top to set variables, and then using eval to run it.