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

I don't know if this is possible, but if it is, how can you pass a QUERY_STRING to the Perl program from the command prompt?

So far I have tried the following:

perl x.pl some=thing perl x.pl?some=thing
Thanx in advance.

-- zdog (Zenon Zabinski)

Replies are listed 'Best First'.
Re: command prompt help
by btrott (Parson) on Jun 06, 2000 at 22:00 UTC
    I gather that you're not using CGI.pm, then, since that first syntax you tried didn't work... if you were using CGI.pm, it would have worked.
    #!/usr/local/bin/perl -w use strict; use CGI; my $q = new CGI; print map $_ . " => " . $q->param($_) . "\n", $q->param;
    See:
    % foo.cgi foo=bar baz=quux foo => bar baz => quux
    So use CGI.pm. The alternative is to write your own code to emulate a CGI environment, reading parameters either from @ARGV or from STDIN. All of which work has already been done in CGI.pm.
RE: command prompt help
by turnstep (Parson) on Jun 06, 2000 at 22:03 UTC

    QUERY_STRING is merely an environment variable, usually set by the web server and passed to your program. For a command line version, just set the environment variable to whatever you want, or use CGI as mentioned above.

    A quick hack to get the program to work as you have typed it (the first version) is to add this to the top of your program:

    $ENV{'QUERY_STRING'} = shift;
    Ugly, but it should work.

    You may also need to add this line:

    $ENV{'REQUEST_METHOD'}="GET";
    depending on what code you are using to parse the input.

Re: command prompt help
by zodiac (Beadle) on Jun 07, 2000 at 21:08 UTC
    As turnstep already pointed out QUERY_STRING is just an environment variable.
    bash $ ( export QUERY_STRING="foo=bar&baz=foo+bar"; perl x.pl) tcsh $ (set QUERY_STRING="foo=bar&baz=foo+bar"; perl x.pl)
    to see a list of all Environment variables available to your script you might want to try the following in as a cgi-script:
    #!/usr/bin/perl $\="\n"; $,="="; print "Content-Type: text/plain\n"; map{print $_, $ENV{$_} } keys %ENV;
Re: command prompt help
by KM (Priest) on Jun 06, 2000 at 21:50 UTC
    If you are using CGI.pm (You are, aren't you???) it will ask you to enter your name/value pairs when you run it from the command line. If you aren't using CGI.pm for this, you should be.

    Cheers,
    KM

      <rant> Why does everyone swear by CGI.pm? For many simple CGI scripts, it is simply overkill. I use a simple little subroutine that creates a hash out of all the name/value pairs at it works fine. </rant>

      I think perhaps the author of the question was wondering how to access command line arguments, and was merely using the CGI example to illustrate what he was saying.

      If so, Perl stores command-line arguments in the @ARGV array.

      If you want to simply treat all the arguments as one, you can just do:

      $arguments = join(' ', @ARGV); # do something with $arguments
      If you want to examine each argument in turn, you can just do:

      foreach $argument (@ARGV) { # Do something with $argument }
        i've been wondering the same thing...i just recently started doing more CGI work, having been doing java abstraction stuff, as well as sysadmin.

        unless you're doing lots of form handling, it seems like major overkill just to parse one QUERY_STRING. and remember the syntax of h1 for you.

        michael d. ivey, <ivey@gweezlebur.com>

        First, CGI.pm will parse the QUERY_STRING so you don't need to, and likely more cleanly than you. Secondly, your little lines of code above will not work with CGI scripts. Why would someone even try to use @ARGV with CGI? Currently, CGI.pm may be slightly bulky, but it does its job well, is maintained, and if it is so expensive for you to use, you need to upgrade your hardware.

        Cheers,
        KM