[thelist] Accessing URL variables in perl

Anthony Baratta Anthony at Baratta.com
Sun Apr 7 23:47:01 CDT 2002

At 09:34 PM 4/7/2002, CDitty wrote:
>How do I access a url variable in perl?  I know that in perl, you can
>pretty much write your own routines for everything.  But I am justa learning.

I would recommend that you use a library to save you lots of coding a new

The two best are cgi-lib.pl and CGI.pm

I like cgi-lib.pl if you are only doing basic stuff, and CGI.pm if you need
heavy lifting. One short coming of cgi-lib is that you can't both post and
get to a CGI program.

Here's an example using cgi-lib.pl


   require "cgi-lib.pl";

## Start off by returing the text-type to the browser
## This will help in preventing a browser timeout
   print &PrintHeader;

## Print HTML Page Header
   print "<HTML><BODY> \n";

## Parsing Form Data for cgi analysis

## Printing out variables received....
   print "<UL><h1>Variables submitted via Post or Get</h1>  \n";
   foreach $key (sort keys %input) {
     print "<b>$key</b> = <i>$input{$key}</i> <br>\n";
   } # end

## Printing out variables received....
   print "<h1>Variables from Environment</h1> \n";
   foreach $key (sort keys %ENV) {
     print "<b>$key</b> = <i>$ENV{$key}</i> <br>\n";
   } # end

## Print HTML Page Footer
   print "</ul></BODY></HTML> \n";


Here's an example using CGI.pm

#! /usr/bin/perl

use CGI;
$query = new CGI;
print $query->header;
print $query->start_html(-title=>"Env Variables");

print $query->h2('Post/Get info') . "\n";
print "<ul>\n";
for $var (sort $query->param){
print $query->b($var) . ": " . $query->param($var) . "<br \>\n";
print "</ul><br><br>\n";

print $query->h2('Environment info') . "\n";
print "<ul>\n";
for $var (sort keys %ENV){
print $query->b($var) . ": " . $ENV{$var} . "<br \>\n";
print "</ul><br><br>\n";
print $query->end_html();

Anthony Baratta
Keyboard Jockeys

"Conformity is the refuge of the unimaginative."

More information about the thelist mailing list