Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

CGI Command Line

by JoeJaz (Monk)
on Apr 12, 2004 at 21:02 UTC ( #344523=perlquestion: print w/ replies, xml ) Need Help??
JoeJaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, Does anyone happen to know of a way to set perl to pass a CGI script POST or GET data from the command line. My friend says that he has used command line switch to put perl into some interperater mode so he could pass perl variable value pairs in the same way that a CGI script could, and therefore test the CGI script from the command line without building a form or adding code to the script. We cannot seem to find such an option in the man pages, however. If anyone has any information as to if this is actually possible and would be willing to share it, I would be very greatful. Thank you very much for reading this. Have a nice day, Joe

Comment on CGI Command Line
Re: CGI Command Line
by pbeckingham (Parson) on Apr 12, 2004 at 21:14 UTC

    For Unix, try:

    export QUERY_METHOD=GET export QUERY_STRING="name=value&x=1"
    then run your CGI program.

      Thanks very much! I'll give that a try right away. I appreciate your time. Joe
Re: CGI Command Line
by barrachois (Pilgrim) on Apr 12, 2004 at 21:16 UTC
    Use the CGI.pm module and follow the directions in its debugging documentation.

    For example, if your script is in the file color.cgi

    #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/plain\n\n"; my $color=param('color'); print "Your chosen color is $color.\n";
    then from the command line you can type
     ./color.cgi color=red
    
    to see it run as it would from a URL like color.cgi?color=red

    - barrachois

      I didn't realize that it was that simple. That's very useful Thanks a lot! Joe
      I also like using Data::Dumper when debugging my CGI scripts.
      #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; print "Content-type: text/plain\n\n"; my $color=param('color'); print Dumper($color); exit; print "Your chosen color is $color.\n";
      # ./color.cgi color=red Content-type: text/plain $VAR1 = 'red';
      It's useful when the script is big and all you want is to check a variable in a middle of it.
        Ooh! Very nice! That's soo much easier that writing my own code to do that type of debugging. Thanks for the tip! Joe
Re: CGI Command Line
by bassplayer (Monsignor) on Apr 12, 2004 at 21:16 UTC
    /usr/bin/perl script_name.pl name_1=value_1 name_2=value_2 ... name_n=value_n

    bassplayer

      If only I had know that 6 months ago when I was doing CGI for work ;-) Thanks for your post! Your help is really appreciated. Joe
        If your values contain spaces, you'll either have to use quotes or escape the spaces with backslashes, like this:
        ./script.pl name="some value with spaces" ./script.pl name=some\ value\ with\ spaces

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-07-11 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (226 votes), past polls