Toolserver:Perl/Query string

From mediawiki.org

This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.

You may be tempted to parse your query string with regex:

if ($ENV{'QUERY_STRING'} =~ m/&param=/) {
    # ...
}

Instead, consider build a hash (more info) from the query string:

# http://www.mediacollege.com/internet/perl/query-string.html

my %in;
if (length($ENV{'QUERY_STRING'}) > 0){
    my $buffer = $ENV{'QUERY_STRING'};
    my @pairs = split(/&/, $buffer);
    foreach my $pair (@pairs){
        my ($name, $value) = split(/=/, $pair);
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $in{$name} = $value; 
    }
}

This is almost certainly more robust, and it gets you using hashes, which can only be a Good Thing.

Or, use CGI.pm's Vars() method to do it for you.

Category:Programming languages