I'm having some trouble getting CGI.pm to output to HTML5 instead of XHTML 1.0 or HTML 4.01. When I try "HTML5"
or "HTML 5"
as the -dtd
argument in start_html()
I get a document in HTML 4. I've also tried importing :HTML5
, but that doesn't seem to work either. Any advice?
Here's a fragment from some code where I 'solved' this problem using brute force.
# $html is accumulator for HTML string
my $html;
# <html> tag and <head> section
my $dtd = '<!DOCTYPE html>'; # HTML5 DTD
my $title = "Storage analysis of $HOSTNAME as of $TODAY";
$html .= start_html(
-title => $title,
-style => {
-code => $css,
}
);
# KLUDGE: CGI.pm doesn't support HTML5 DTD; replace the one it puts in.
$html =~ s{<!DOCTYPE.*?>}{$dtd}s;
The correct doctype for HTML 5 is just "html", not "html5" or "html 5", and does not use a DTD. CGI.pm only supports well-formed DTDs, not arbitrary strings. Since the HTML 5 doctype does not include a well-formed DTD, CGI.pm (as of the current version, 3.49) does not support the HTML 5 doctype.
Using CGI.pm's HTML-generation functions is generally frowned upon these days. Templating systems such as Template::Toolkit or HTML::Template are preferred for their ability to cleanly separate your code's logic from the formatting of its output. They also, incidentally, allow you to specify whatever doctype and code to whatever version of (X)HTML you choose.
Here are some Perl5 frameworks that are HTML5 friendly:
Catalyst http://www.catalystframework.org/ Dancer http://perldancer.org/documentation Mojolicious http://mojolicio.us/
I'm leaning toward using Mojolicious for my newest Perl project.
All of these are more relevant for robust HTML5 apps than the CGI module. CGI still has its place and is still developed/supported but it does not address robust HTML5 apps as well as some of the frameworks that are out there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With