Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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

Replies are listed 'Best First'.
Re: CGI Command Line
by barrachois (Pilgrim) on Apr 12, 2004 at 21:16 UTC
    Use the 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 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
      I didn't realize that it was that simple. That's very useful Thanks a lot! Joe
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 bassplayer (Monsignor) on Apr 12, 2004 at 21:16 UTC
    /usr/bin/perl name_1=value_1 name_2=value_2 ... name_n=value_n


      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:
        ./ name="some value with spaces" ./ name=some\ value\ with\ spaces

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://344523]
Approved by Corion
[Discipulus]: very misleading.. Now I start understand why I'm always off one pixel..

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2018-04-23 11:13 GMT
Find Nodes?
    Voting Booth?