Sunday, July 24, 2011

getting variables from GET method in perl (cgi)

Suppose we have a page with the following form:

<form action="" method="get">
<input type="text" name="search_for" size="30" />
<input type="submit" value="search" />

So, there's a text input field named "search_for", and a submit button.
The following code gets the variables set in QUERY_STRING (whatever is after, into the $arg associative array. So we can get the value of "search_for" by $arg{"search_for"}.

for (split /\&/, $ENV{QUERY_STRING}) {
($key,$val) = split /\=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
$arg{$key} = $val;
print "data searched for is =

No comments:

Post a Comment