English 中文(简体)
HTTP::带有say的标头
原标题:HTTP::Headers with say
  • 时间:2012-05-27 16:54:57
  •  标签:
  • perl

以下是Net::Server::HTTP的简单脚本

#!/usr/bin/env perl

use strict;
use warnings;

use v5.12;

use HTTP::Headers;

use base qw(Net::Server::HTTP);
__PACKAGE__->run(port =>  localhost:8421 );

sub process_http_request
{
  my ($self) = @_;
  my $header = HTTP::Headers->new();

  $header->content_type( text/html );

  print $header->as_string() . "
"; # <- this works
  # say $header->as_string() . "
"; # <- this doesn t work and I still have to add a newline

  say "<!doctype html>";
  say "<html>";
  say "<head>";
  say "  <title>Test</title>";
  say "</head>";
  say "<body>";
  say "<h1>Test</h1>";
  say "</body>";
  say "</html>";
}

我假设通过使用比如,我可以绕过添加换行符的问题,但我必须添加它,它通过在每个HTTP字段中添加另一个换行符来破坏HTTP::Headeras_strings建议它是一个字符串。有人能解释一下这里到底发生了什么吗?

最佳回答

这个bug似乎是软件没有正确地委托给Net::服务器::绑定句柄的SAY方法,仅用于打印。我不知道如何修复此问题。

我建议您使用Plack用于Web应用程序。

fileapp.psgi

my $html = << HTML ;
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Test</h1>
</body>
</html>
HTML

my $app = sub {
    return [200, [Content_Type =>  text/html ], [$html]];
};

命令行:

plackup --port 8421
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签