Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Passing Variables in CGI

by drujax (Initiate)
on Jul 24, 2000 at 18:13 UTC ( #24065=perlquestion: print w/replies, xml ) Need Help??
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 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 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">
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 and is located in you /cgi-bin/ directory.

    You can call your script like so:

    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
    perl 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:

    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 Not using is dangerous as CGI parameter extraction is a tricky business and very easy to get wrong. Using, 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:


    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


    European Perl Conference - Sept 22/24 2000, ICA, London
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:
    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, 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

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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://24065]
Approved by root
[marioroy]: LA my @music_times = qx(find \$HOME/Music/ Albums -type f -iname \'*.mp3\' -exec mp3info -p \"%S\\n\" {} \\;);
[marioroy]: Only 2 backslash \\n
[marioroy]: LA no space before Albums
[Lady_Aleena]: Good guh! I think I will use File::Find when working in perl. That is too many toothpicks.
[marioroy]: mp3info must be in the PATH
[marioroy]: LA ++
[Lady_Aleena]: marioroy, this is forcing me to learn perl modules to use instead.
[marioroy]: qx isn't fun if having to backslash ' and " and \.
[marioroy]: LA ++ ok.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-04-23 21:26 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.