url encode
use strict;
use URI::Escape;
print urlEncode( (a=>12,b=>"john smith"));
#input: (key,value) paris, e.g. (a=>12,b=>"john smith");
#output: url querystring. a=12&b=john%20smith
sub urlEncode
{
my @data=@_;
my $len=$#data+1;
my @pair;
for(my $i=0; $i<$len/2; $i++) {
my $name=$data[2*$i];
my $val=$data[2*$i+1];
push @pair, "$name=" . uri_escape($val);
}
return join("&", @pair);
}
url decode
use strict;
use URI::Escape;
print join(" ", urlDecode("a=12&b=john%20smith"));
#input: url querystring. a=12&b=john%20smith
#output: (key,value) paris, e.g. (a=>12,b=>"john smith");
sub urlDecode
{
my $query=shift;
my @pairs=split("&", $query);
my @all;
foreach my $pair (@pairs) {
my ($name, $val)=split("=", $pair);
$val=uri_unescape($val);
push @all, $name, $val;
}
return @all;
}