Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

Up to now I haven't been haveing much trouble with getting the arguments for a script by using @ARGV or shift(). However, once I tried getting arguments that have the '=' (equal sign) in it, I no longer get anything. Apparently these sort of arguments are handled differently, and all I want to know is how to get the name and what it equals.

For example:
script.pl?number=100
The things I want to get are 'number' and '100'. How do I do that?

- Monolith

  • Comment on Getting arguments such as ?page=20 for web scripts

Replies are listed 'Best First'.
Re: Getting arguments such as ?page=20 for web scripts
by grep (Monsignor) on Jan 08, 2002 at 22:53 UTC
    The first this you want to do is use CGI;. The CGI.pm is the standard/recommended/highly praised way of access the arguments you seek.
    Once you have 'use CGI;' at the top of your script then it is very simple

    #!/usr/bin/perl -Tw use strict; use CGI; my q$ = new CGI; my $number = $q->param('number');

    Make sure you perldoc CGI to read the docs for CGI.pl

    grep
    grep> cd pub grep> more beer
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Getting arguments such as ?page=20 for web scripts
by Chmrr (Vicar) on Jan 08, 2002 at 22:56 UTC

    As others have already leaped to point out, the CGI module is your friend. I would point you to our own Ovid's CGI course for a gentle introduction to writing CGI scripts, with a keen eye on the security thereof.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Getting arguments such as ?page=20 for web scripts
by merlyn (Sage) on Jan 08, 2002 at 22:50 UTC
Re: Getting arguments such as ?page=20 for web scripts
by cacharbe (Curate) on Jan 08, 2002 at 22:52 UTC
    Read the CGI docs. That is a good beginning.

    C-.

      The CGI module does many things. What is it that I am looking for?

      - Monolith

        CGI.pm has a method, param(), which when called with no arguments, returns a list of CGI parameters. When called with one of the parameters as an argument, param() returns its value. The following scriptlet gets its parameters and prints their values, in quotes.

        use CGI; my $q = new CGI; # so the output won't be interpreted as HTML print $q->header('text/plain'); print "$_ = '", $q->param($_), "'\n" for $q->params;

        Update: Oops! left out the second call to param()!

        dmm

        You can give a man a fish and feed him for a day ...
        Or, you can
        teach him to fish and feed him for a lifetime