CGI and Parameters

You can use CGI to get and set the parameters passed to your script in the URL string:

your_script?foo=bar&baz=buzz&baz=bizz

or POSTed in fill-out forms.

Function-Oriented Way

$bar = param('foo');   # scalar
$baz = param('baz');   # scalar
@baz = param('baz');   # list

param('foo' => 'BIG BAR');  # set foo to scalar
param('foo' => ['VERY','BIG','BAR']);  # set foo to list

Object-Oriented Way

$q = new CGI;
$bar = $q->param('foo');

You can create multiple independent CGI objects and save and restore their state:

$q = new CGI;
$r = new CGI;  # starts out being the same as $q
$r->param('foo' => ['VERY','BIG','BAR']);  # now $r is different

# save state to filehandle
open (FILE,">/tmp/cgi.txt");
$r->save(\*FILE);       

# restore state from filehandle
open (F,"/tmp/cgi.txt");
$i = new CGI(\*F);

# save state to URL
$query = $q->query_string();

# restore from URL
$j = new CGI($query);

Handy Tied Interface

Vars() gives you a nice tied interface to parameters:

$q = new CGI;
$v = $q->Vars;
print $v->{'foo'};
print $v->{'bar'};
@baz = split "\0",$v{'baz'};

<< Previous Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Mon Aug 17 10:46:14 EDT 1998