Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
[1nickt]: Corion this looks good, from the pod: "there are 9000+ variations that are detected correctly in the test files (see t/data/* for most of them). If you can think of any that I do not cover, please let me know."
[TCLion]: some moron put the date like this : Mon Feb 20 09:31:30 2017
[Corion]: 1nickt: Yes, the module sounds promising indeed
[1nickt]: "putting the date in correct order" how?
[Corion]: TCLion: Whee ;)
[TCLion]: need to put like this : 2017-02-20 09:30:53
[1nickt]: That's why I asked if you are using DateTime. It has a large number of supporting modules (the author likes the term 'eco-system') so if you are already creating a DateTime obj from your dates, this module would read in the mnoron-formatted 1s seamlessly
[TCLion]: when I put the date together it looks like : 2017-Feb-24 (month is the problem)
[1nickt]: good luck, then.
[TCLion]: looking at DateTime documentation in monastery now

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-03-23 14:56 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (288 votes). Check out past polls.