URI::Encode(3pm)
π URI::Encode β bestie, let's talk percent-encoding β¨
okay so this is the manual page for URI::Encode (that's the Perl module, URI::Encode(3pm), from the User Contributed Perl Documentation crew π
). It's a cute little module that does simple URI (percent) encoding and decoding, and honestly? It ate. Let's get into it π«Ά
πΈ NAME πΈ
URI::Encode β Simple percent Encoding/Decoding. That's the whole name, no notes, periodt β¨
π¦ SYNOPSIS π¦
Two ways to use it: the OOP girlie route, and the functional route. Both iconic, pick your fighter π
# OOP Interface
use URI::Encode;
my $uri = URI::Encode->new( { encode_reserved => 0 } );
my $encoded = $uri->encode($data);
my $decoded = $uri->decode($encoded);
# Functional
use URI::Encode qw(uri_encode uri_decode);
my $encoded = uri_encode($data);
my $decoded = uri_decode($encoded);
π DESCRIPTION π
This module provides simple URI (Percent) encoding/decoding. Lowkey exactly what it says on the tin π
The main purpose of this module (at least for the author, and honestly same energy) was to give you an easy method to encode strings β mainly URLs β into a format that can be pasted into plain text emails, where those links stay 'click-able' for whoever's reading that email π. You pull this off by NOT encoding the reserved characters. No cap, that's the trick.
It's also useful when you're working with HTTP::Tiny and you want to make sure your URLs are properly escaped π§
Heads up bestie: this module does not encode reserved characters by default. If you're chasing speed and you do want reserved characters encoded, go use URI::Escape::XS instead π
There's a script at https://github.com/mithun/perl-uri-encode/raw/master/.author/benchmark.pl that compares encoding results and performance, if you're nosy like me π
π METHODS π
β¨ new() β¨
Creates a new object β no arguments required, she's low-maintenance πΈ
my $encoder = URI::Encode->new(\%options);
The following options can be passed to the constructor:
encode_reservedβmy $encoder = URI::Encode->new({encode_reserved => 0});
If true, the "Reserved Characters" get encoded too. Defaults to false. π
double_encodeβmy $encoder = URI::Encode->new({double_encode => 1});
If false, characters that are already percent-encoded will not be encoded a second time. Defaults to true. So check it:
my $encoder = URI::Encode->new({double_encode => 0});
print $encoder->encode('http://perl.com/foo%20bar'); # prints http://perl.com/foo%20bar
β¨ encode($url, \%options) β¨
This method encodes the URL you hand it. The $url you provide gets converted into UTF-8 first, and then percent encoded π Options set in the constructor β or the defaults β can be overridden by passing them as the (optional) second argument. Those options must be a hashref, no substitutes π
$uri->encode("http://perl.com/foo bar");
$uri->encode( "http://perl.com/foo bar", { encode_reserved => 1 } );
β¨ decode($url) β¨
This method decodes a 'percent' encoded URL π¦ If you encoded the URL using this module (or literally any other method), chances are the URL was converted to UTF-8 before the 'percent' encoding happened. So be sure to check the format and convert back if you need to β fr fr, don't skip that step π
$uri->decode("http%3A%2F%2Fperl.com%2Ffoo%20bar");
π EXPORTED FUNCTIONS π
The following functions are exported upon request. This is the non-OOP interface, for when you just want the functions and none of the object drama π
uri_encode($url, \%options)uri_decode($url)
β CHARACTER CLASSES β
πΈ Reserved Characters πΈ
These characters are considered reserved, per RFC 3986 (http://tools.ietf.org/html/rfc3986). They only get encoded if you request it β otherwise they stay unbothered and thriving π
! * ' ( ) ; : @ & = + $ , / ? # [ ]
π¦ Unreserved Characters π¦
These ones are considered Unreserved. They will not be encoded, periodt:
a-z
A-Z
0-9
- _ . ~
π DEPENDENCIES π
Encode
π«Ά ACKNOWLEDGEMENTS π«Ά
- Gisle Aas for
URI::Escape - David Nicol for
Tie::UrlEncoder
shoutout to the legends, we love to see it π
π₯ SEE ALSO π₯
- RFC 3986 β
http://tools.ietf.org/html/rfc3986 URI::EscapeURI::Escape::XSURI::Escape::JavaScriptTie::UrlEncoder
π BUGS AND LIMITATIONS π
Please report any bugs or feature requests at https://github.com/mithun/perl-uri-encode/issues π Don't suffer in silence bestie, tell the maintainer.
π AUTHOR π
Mithun Ayachit mithun@cpan.org β¨
π LICENSE AND COPYRIGHT π
Copyright (c) 2014, Mithun Ayachit. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. π
and that's the whole page, bestie!! this one comes to you from the User Contributed Perl Documentation corner of the world, perl v5.36.0, dated 2022-10-13 β so yes, it's giving slightly vintage but she still serves π go forth and percent-encode responsibly, mwah πβ¨π¦
π¦ where this page comes from
- ubuntu noble: liburi-encode-perl 1.1.1-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: liburi-encode-perl 1.1.1-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: liburi-encode-perl 1.1.1-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms