【问题标题】:Perl CGI Meta tagsPerl CGI 元标记
【发布时间】:2014-05-22 00:21:31
【问题描述】:

http://perldoc.perl.org/CGI.html制作元标记,给出如下示例:

print start_html(-head=>meta({-http_equiv => 'Content-Type',-content => 'text/html'}))

但是使用以下代码:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $cgi = new CGI;
$cgi->autoEscape(undef);
$cgi->html({-head=>meta({-http_equiv => 'Content-Type',-content => 'text/html',-charset=>'utf-8'}),-title=>'Test'},$cgi->p('test'));

我收到以下错误:

$ perl test.cgi 未定义的子程序 &main::meta 在 test.cgi 第 8 行调用。

我正在尝试生成以下标签:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

【问题讨论】:

  • 请停止使用肮脏的旧 CGI.pm。请改用现代且干净的 Web 引擎,例如 DancerMojolicious

标签: perl cgi


【解决方案1】:

use CGI; 时,meta 子程序不会自动导入。试试

use CGI "meta";

(或":all")。

【讨论】:

    【解决方案2】:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI qw(:all);
    
    my $cgi = new CGI;
    $cgi->autoEscape(undef);
    $cgi->charset('utf-8');
    print
        $cgi->start_html(
            -head  => meta({-http_equiv => 'Content-Type', -content => 'text/html'}),
            -title => 'Test'
        );
    

    但是,您是否 100% 确定要使用 CGI 进行 Web 开发而不是更好的东西,例如 PSGI/Plack?

    【讨论】:

    • 选择有限,我正在制作一个在 Netcool/Omnibus 内部工作的页面,它使用自己的 Websphere HTTP 服务器,它与 Perl 配合得最好。
    • PSGI/Plack IS perl。 ;) 但是,好吧,我不知道 Omnibus。无论如何,如果 Omnibus 可以充当代理,这里应该没有问题,因为您只是将请求代理到 Plack。而且 Plack 确实加快了任何 perl web 应用程序的开发。 ($0.02) :)
    【解决方案3】:

    这篇文章很老了,但解决方案很简单:元是对象 $cgi 的方法,所以将其用作方法。

    你的例子

    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI;
    
    my $cgi = new CGI;
    $cgi->autoEscape(undef);
    $cgi->html({-head=>$cgi->meta({-http_equiv => 'Content-Type',-content=>'text/html',-charset=>'utf-8'}),-title=>'Test'},$cgi->p('test'));
    

    我只是在 meta 的 fromt 中添加了 $cgi->。

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 2013-12-21
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      相关资源
      最近更新 更多