Tie::UrlEncoder - interpolatably URL-encode strings
Syntactic sugar for URL-Encoding strings. Tie::UrlEncoder imports a tied hash %urlencode into your package, which delivers a RFC 1738 URL Encoded string of whatever is given to it, for easy embedding of URL-Encoded strings into doublequoted templates.
%urlencode
our %urlencode; # make use strict happy use Tie::UrlEncoder 0.01; # import ties %urlencode ... print "To add $id to your list, click here:\n"; print "http://listmonger.example.com/listadd?id=$urlencode{$id}\n";
No longer must you clutter up your CGI program with endless repetitions of line noise code that performs this tricky function. Simply use Tie::UrlEncoder and you instantly get a magic %urlencode hash that gives you an Url Encoded version of the key: $urlencode{$WhatYouWantToEncode} is ready to interpolate in double-quoted literals without messy intermediate variables.
$urlencode{$WhatYouWantToEncode}
you get our %urlencode imported into your package by default.
our %urlencode
Defeat this wanton pollution (perhaps if you already have something called %urlencode) by invoking use with an empty list and tieing a different hash.
use
use Tie::UrlEncoder 0.01 (); tie my %MagicUrlEncodingHash, 'Tie::UrlEncoder'; ... qq( <a href="add_data.pl?data=$MagicUrlEncodingHash{$SpecialData}"> Click here to add your special data <em>$SpecialData</em></a> );
I was setting this up for a project I am working on and thought, it's useful in general so why not publish it.
silence a warning that has appeared with 5.10 (rt #35807)
A hash-tieing interface for HTML escapes is available as HTML::Entities::Interpolate
RFC 1738 says:
In addition, octets may be encoded by a character triplet consisting of the character "%" followed by the two hexadecimal digits (from "0123456789ABCDEF") which forming the hexadecimal value of the octet. (The characters "abcdef" may also be used in hexadecimal encodings.) Octets must be encoded if they have no corresponding graphic character within the US-ASCII coded character set, if the use of the corresponding character is unsafe, or if the corresponding character is reserved for some other interpretation within the particular URL scheme.
so, 0.2 includes a use bytes before the substitution operator. Research indicates that the bytes pragma first appeared in 5.006, which is the version of perl that v0.01 wants already, so the techniques at http://www.perlmonks.org/?node_id=294501 are not needed.
use bytes
bytes
Copyright (C) 2004, 2009 david nicol davidnico@cpan.org released under your choice of the GNU Public or Artistic licenses
Google for "URL Encoding"
RFC 1738
URI::Escape
HTML::Mason::Escapes
To install Tie::UrlEncoder, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::UrlEncoder
CPAN shell
perl -MCPAN -e shell install Tie::UrlEncoder
For more information on module installation, please visit the detailed CPAN module installation guide.