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

Hello monks.

I have a CGI script that is supposed to be used via a web browser and a form submit. However, I would love to test it out using the command line rather than the web site. How can I feed my script the form name/value pairs as a command line argument?

Thanks!
-X
  • Comment on Testing out a CGI script using the command line

Replies are listed 'Best First'.
Re: Testing out a CGI script using the command line
by Lucky (Scribe) on Oct 09, 2002 at 06:39 UTC
    It's a quite simple task. Run you script then enter CGI parameters:

    name=value
    name=value
    Ctrl+D

    You can export parameters from file:

    $ perl -d your_script.cgi < file_with_parameters

    Lucky

Re: Testing out a CGI script using the command line
by grantm (Parson) on Oct 09, 2002 at 08:20 UTC

    One more tip: you can also specify the parameters as command line arguments in standard querystring format. One thing to watch out for though is that on Unix/Linux, the shell will see the '&' separator as an instruction to run the script in the background. To avoid that, use single quotes:'

    myscript.cgi 'param1=hello&param2=world'
Re: Testing out a CGI script using the command line
by joe++ (Friar) on Oct 09, 2002 at 08:44 UTC
    Hi,
    you didn't mention you're using CGI.pm - If not, that's the recommended way (see previous tips).

    However, if this is not possible for some reason, you can write a simple wrapper which emulates the relevant CGI Environment Variables.

    This is an (untested) example for emulating the GET environment, which should work for non-perl CGI's as well:

    print &get_response("/path/to/cgi.pl", "test=true"); sub get_response { my ($cgi, $CMD) = shift; ## prepare proper (CGI) environment $ENV{QUERY_STRING} = $CMD; $ENV{REQUEST_METHOD} = 'GET'; $ENV{GATEWAY_INTERFACE} = 'CGI/1.1'; # add more as needed open(CGI, "$cgi |") or die "Can't exec $cgi, $!"; local $/ = undef; my $res = <CGI>; close(CGI) or die "Error running $cgi, $!"; return $res; }

    --
    Cheers, Joe

      Thanks for all of your help. I was using another person's script and this person did NOT use CGI.pm, so thanks for giving me that helpful example. I guess I'll modify the script to use the CGI module.

      -X
Re: Testing out a CGI script using the command line
by htoug (Deacon) on Oct 09, 2002 at 06:34 UTC
    Just read the very fine manpage for CGI!

    Look for the term DEBUGGING (it's quite a way down, approx 81%).