Skip to content

Instantly share code, notes, and snippets.

@sids
Created October 29, 2008 09:10
Show Gist options
  • Save sids/20661 to your computer and use it in GitHub Desktop.
Save sids/20661 to your computer and use it in GitHub Desktop.
Perl: URI encoding/decoding
#!/usr/bin/perl
use strict; use warnings;
use URI::Escape;
# http://www.perlhowto.com/encode_and_decode_url_strings
my $string = "Hello world!";
my $encode = uri_escape($string);
print "Original string: $string\n";
print "URL Encoded string: $encode\n";
#!/usr/bin/perl
use strict; use warnings;
# http://support.internetconnection.net/CODE_LIBRARY/Perl_URL_Encode_and_Decode.shtml
# encode:
$uri =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
# decode:
$uri =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment