You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让CGI.pm输出HTML5而非XHTML 1.X?

用CGI.pm生成HTML5页面的简易解决方案

我需要制作一些简单的HTML页面,但遇到了CGI.pm默认输出严格的XHTML 1.0或HTML 4.01而非HTML5的问题。我看过相关讨论,但既没时间也不想为完成这个任务去学习模板框架。我清楚CGI.pm的README里说过不该再用它的HTML生成函数,但现代浏览器仍广泛支持它生成的内容,所以我用了个取巧的暴力方法解决问题,以下是混合使用HTML5与CGI.pm HTML生成函数的完整测试脚本示例。

#!/usr/bin/perl

use CGI ':all';
use CGI::Carp 'fatalsToBrowser', 'carpout';


# $html5 is accumulator for HTML string
my $html5;

# <html> tag and <head> section
my $dtd      = '<!DOCTYPE html>';   # HTML5 DTD
my $intro    = '<html lang="en">';
my $title    = "This is a test of HTML5 compliant headers using CGI.pm";
my $css3     =  "/screen.css";

$html5    .= start_html(
    -title  => $title,
    -style   => {
                 'src' => "$css3",
                }
);

# KLUDGE: CGI.pm doesn't support HTML5 DTD or the xmlns in the <html> tag; replace the ones it uses

$html5    =~  s{<!DOCTYPE.*?>}{$dtd}s;
$html5    =~  s{<html.*?>}{$intro}s;

print  header(),
       "$html5",
       "This is a test of the HTML5 required textfield entry.",
       start_form(-action=>'/cgi-bin/processform.pl'),
       p(),
       "Name:  <input type='text' name='name' required>",
       p(),
       submit(-name=>'submit',-value=>'Submit',-id=>'submit'),
       p(),
       end_form(),
       end_html();

exit(0);

内容的提问来源于stack exchange,提问作者Tim Lank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 15:34:56