In this article I'm just going to show you a little script that will make working with forms and cookies in Perl a little easier. The form information, and any cookies present will already be dumped into variables matching their names, like in PHP.

Time Saving

I don't know about you, but when I'm coding in Perl I used to get sick of breaking the query string, POST input, cookie information up so i could get at it. This little script can be included at the top of each of your perl script to make it just that little bit easier. I know a lot of people have probably already got something similar for web programs with Perl, but I'll put it up here anyway.

Here it is

Process GET/Query String

#!/usr/bin/perl

# Process form variables for both POST and GET methods.

# Below Will process all query string variables.

if ($ENV{'REQUEST_METHOD'} eq "GET" $ENV{'QUERY_STRING'} ne "")

{

@pairs = split(/\&/, $ENV{'QUERY_STRING'});

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair); # Split into name and value.

$$name = $value; # Assign value to scalar matching name.

$$name =~ s/%(..)/chr(hex($1))/ge; # Decode encoded stuff.

$$name =~ s/\+/ /g; # substitute +'s for spaces.

}

}

If the request method is GET the variables will be in the query string, all query string information will be processed by this bit of code whether it's from a form or not. We split the query string at the & and dump each pair into the @pairs array. Then we use the foreach function to go through each pair in the array, split them at the = and dump the first part into $name and the last part into $value. Then we assign the $value variable to a scalar variable matching the name of the $name variable. Say we had in the query string: ?action=mail, we would now have a variable $action containing "mail". Finally we decode anything that has been URL encoded, and replace any + signs with a space. Remember to format your query string correctly when using this script. Just http://host.com/index.pl?mail will cause weird things to happen. Assign "mail" to a handler ?action=mail.

Process POST

# Process POST form variables.

if ($ENV{'REQUEST_METHOD'} eq "POST")

{

read(STDIN, $stuff, $ENV{'CONTENT_LENGTH'});

@pairs = split(/\&/, $stuff);

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair);

$$name = $value;

$$name =~ s/%(..)/chr(hex($1))/ge;

$$name =~ s/\+/ /g;

}

}

Reads the POST information into the variable $stuff and then processes them in the same way we did the query string, assigning the value to a scalar variable matching the form input name.

Process Cookies

# Process cookies.

if ($ENV{'HTTP_COOKIE'} ne "")

{

@pairs = split(/\; /, $ENV{'HTTP_COOKIE'});

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair);

$$name = $value;

$$name =~ s/%(..)/chr(hex($1))/ge;

$$name =~ s/\+/ /g;

}

}

This time, if $ENV{'HTTP_COOKIE'} contains data we split the information at the ; and then process it the same way we processed the POST and GET/query string information.

Include it

Just include the script at the top of each of your perl files.

do "$DOCUMENT_ROOT/script.pl";

The Full Script

#!/usr/bin/perl

# Process form variables for both POST and GET methods.

# Below Will process all query string variables.

if ($ENV{'REQUEST_METHOD'} eq "GET" $ENV{'QUERY_STRING'} ne "")

{

@pairs = split(/\&/, $ENV{'QUERY_STRING'});

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair); # Split into name and value.

$$name = $value; # Assign value to scalar matching name.

$$name =~ s/%(..)/chr(hex($1))/ge; # Decode encoded stuff.

$$name =~ s/\+/ /g; # substitute +'s for spaces.

}

}

# Process POST form variables.

if ($ENV{'REQUEST_METHOD'} eq "POST")

{

read(STDIN, $stuff, $ENV{'CONTENT_LENGTH'});

@pairs = split(/\&/, $stuff);

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair);

$$name = $value;

$$name =~ s/%(..)/chr(hex($1))/ge;

$$name =~ s/\+/ /g;

}

}

# Process cookies.

if ($ENV{'HTTP_COOKIE'} ne "")

{

@pairs = split(/\; /, $ENV{'HTTP_COOKIE'});

foreach $pair (@pairs)

{

($name, $value) = split(/=/, $pair);

$$name = $value;

$$name =~ s/%(..)/chr(hex($1))/ge;

$$name =~ s/\+/ /g;

}

}

Expand It

If you're using Perl for CGI why not use this little script, and add to it some? Maybe you could define the content-type header in the script, as you'll probably be using HTML just add:

print "Content-type: text/html

";

so you don't need to bother doing it in your scripts. Why not add a mailing subroutine into it (see MartinB's Article, A Simple CGI E-Mail Subroutine). Be creative.

Bibliography

Well, there's no links i can point you to really. Hummm, how about some Perl stuff?