Snafui.com - Perl: Decoding CGI URL Encoded Param String

Problem

A URL encoded CGI param string needs to be decoded. In URL encoding, special characters get translated into their hex equivilents. A period becomes %2e, and / becomes %2f. Here's a handy function to get them translated. It was shamefully taken from the NoCat Project in their NoCat.pm module. I modified it a bit to remove the OO Perl aspects from it.

Solution

sub url_decode {
	my @args = @_;
	s/%([0-9A-F]{2})/chr hex $1/egios for @args;
	return wantarray ? @args : $args[0];
}
To use it:
print url_decode('url=http%3a%2f%2fsnafui%2ecom');
will print out:
url=http://snafui.com