这是 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() . "\n"; # <- this works
# say $header->as_string() . "\n"; # <- 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>";
}
我假设通过使用say
我可以绕过添加换行符,但是我必须添加它并且它会HTTP::Header
通过向每个 HTTP 字段添加另一个换行符来破坏 ,这没有意义,因为as_string
它暗示它是一个字符串。有人可以解释一下这里到底发生了什么吗?