Getting the HTML and URI escaping right for creating links and labels is fun, not!
But if you've got the right template to start from, it's not so bad. Here's a basic snippet. Handles whitespace correctly, as well as %, &, <, and >. Beware
hand-rolled solutions which don't.
#!/usr/bin/perl
use CGI qw(:all);
use URI::Escape;
print header, start_html("sample"), h1("sample");
print map { a({ -href => uri_escape($_)}, escapeHTML($_)), br } glob "
+*";
print end_html;
Note that the HTML
a shortcut does an
escapeHTML on the HREF
attribute automatically. The manual equivalent would be:
print
'<A HREF="',
escapeHTML(uri_escape($_)),
'">',
escapeHTML($_),
'</A>';
Yes, all that escaping
is necessary. Do not shirk.