http://www.perlmonks.org?node_id=24065

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

I am still fairly new to perl and i was wondering if someone could help me with my current problem. I am wondering how you pass variable information via the URL and how you can retrieve it. Thanks in advance for any help! -dru

Replies are listed 'Best First'.
RE: Passing Variables in CGI
by steveAZ98 (Monk) on Jul 24, 2000 at 18:26 UTC
    The easiest way is to pass the information via a querystring.
    ie. http://hostname/perl/script?a=b&c=d etc.
    or in a form.
    <form action="/perl/scriptname" method="post"> <input type="text" name="a" value=""> <input type="submit" value="Submit"> </form>
    To retrieve the values it's easiest to use CGI.pm like the following.
    use CGI; my $q = CGI->new(); my $a = $q->param('a');
    There are many ways to retrieve values. This is the easiest one for me, others may have their own opinion.
    There is a lot more you can do with CGI.pm so read the documentation on it.
      Slight problem in your post. If you wish to pass parameters in the URL, your method should be get, not post. You can still append data to the URL and it will be processed with get, but your post does not make that clear. Here's the correct form tag:
      <form action="/perl/scriptname" method="get">
      Cheers,
      Ovid
Re: Passing Variables in CGI
by ar0n (Priest) on Jul 24, 2000 at 18:27 UTC
    A simple example:
    #!/usr/bin/perl -w use CGI qw(:standard); use strict; my $query = new CGI; my $name = $query->param('name'); my $loc = $query->param('loc'); print $query->header("text/plain"); print "$name is from $loc\n";
    Now suppose this script is called tst.pl and is located in you /cgi-bin/ directory.

    You can call your script like so:
    http://www.host.com/cgi-bin/tst.pl?name=Joe&loc=The+Netherlands

    A '+' substitutes a space in a query, and name=value pairs are seperated by a &
    Be sure your server has executable permissions on your script, so you won't get "500 Server Error." However, if you do encounter errors, it would be wise to start it from the command line. That's real easy to do because you're using CGI.pm:
    perl tst.pl name=Joe&loc=The+Netherlands

    update: i should reload more often. if this is redundant, sorry.

    -- ar0n
Re: Passing Variables in CGI
by davorg (Chancellor) on Jul 24, 2000 at 18:34 UTC

    To pass data via a URL you would use something like this:

    http://not.dave.org.uk/cgi-bin/script.pl?key1=val1&key2=val2

    The parameters are appended to the end of the URL following a question mark (?). Parameters are arranged in key/value pairs where key and value are separated by an equals sign (=). Each pair is spearated from the next with an ampersand (&). Note that certain characters (including ?, = and & for obvious reasons) cannot be used in a URL and must therefore be encoded.

    Within the CGI program, extracting the parameters is very easy as long as you use CGI.pm. Not using CGI.pm is dangerous as CGI parameter extraction is a tricky business and very easy to get wrong. Using CGI.pm, you can just write code like this:

    #!/usr/bin/perl -w use strict; use CGI qw(:standard); print header(-type => 'text/plain'); foreach (param) { print "Parameter $_ is ", param($), "\n"; }

    Note that it is perfectly legal to have multi-valued CGI parameters, like this:

    key1=val1&key2=val2a&key2=val2b

    In this case the param function will return a list when asked for parameter key2. In this case the sample code above will break.

    Get more info on this from perldoc CGI or Lincoln Stein's book The Official Guide to Programming with CGI.pm.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
Re: Passing Variables in CGI
by gaggio (Friar) on Jul 24, 2000 at 18:25 UTC
    Place your parameters at the end of the URL, after a question mark, separated by the character '&'.

    For example, if I want to pass the parameters 'param1' and 'param2' with the values 'value1' and 'value2', I would do:

    http://www.myurl.com?param1=value1&param2=value2
    You can then access the values in the CGI after having written a function to parse the environment (it is already done for you if you use the module CGI.pm, so use this module).
RE: Passing Variables in CGI
by Russ (Deacon) on Jul 25, 2000 at 01:11 UTC
Quick N Dirty
by Nitsuj (Hermit) on Jul 25, 2000 at 00:07 UTC
    Well, I suppose that you could do it by hand, but that won't get you results as quick as using CGI.pm

    If you search CGI on this site, you will be brought to the documentation page that comes with CGI.pm. This can also be generated from CGI.pm file in several different formats, for details on doing this (there are a few options, manpages, HTML, so forth), look in the comments of CGI.pm.
    GOOD LUCK!

    "We're all different!"
    "I'm not"
    -The Life of Brian