开发者

How do I get CGI.pm to output HTML5 instead of XHTML 1.0?

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?


  1. 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.

  2. 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'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;


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.


Patch the module to add support for HTML5 … or just output a Doctype manually, then use it as normal. If it is valid XHTML 1.0 or HTML 4.01 then it is valid HTML 5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜