perl:无法打印到 object 中的文件句柄?

perl: Can't print to filehandle in object?

我正在尝试调试一个不报告错误但浏览器显示生成的文本而不是呈现页面的 cgi 脚本。我从容器 object(某种程度上)调用了 cgi 来查看我是否发送了 header 两次。

package debugcgi;

use CGI qw(:standard);
use CGI qw(:standard Vars);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

sub new {
    my ($class,$glob) = @_;
    open(lls,">process-cgi.txt");
    return bless{'cgi'=>CGI->new($glob),'glob'=>\*lls,'headers'=>0},$class;
}

sub header {
    my $self = shift;
    my $tmp = shift->{'cgi'}->header(@_);
    print $tmp;
    my $t = $self->{'glob'};
    print $t $tmp;
    $self->{'headers'}++;
}
...

sub myclose {
    my $self = shift;
    my $t = $self->{'glob'};
    my $tmp = $self->{'headers'};
    print $t "\nnumber of headers: ";
    print $t $tmp;
    close $t;
}

1;

用作错误脚本中真实 cgi 的简单替代品:

use debugcgi;
...
#my $cgi = CGI->new(\*STDIN);
my $cgi = debugcgi->new(\*STDIN);
... 
print $cgi->header(Referer => $cgi->url());

哦。

但是除了 "number of headers: 0" 之外,没有任何内容打印到文件中,我仍然得到一个完整的 HTML 文档来显示。我做错了什么,我该如何改进?

你这里有问题:

sub header {
    my $self = shift;
    my $tmp = shift->{'cgi'}->header(@_);
    ...

$self 是您的散列,其中包含 cgi object。所以,你需要这样做:

sub header {
    my $self = shift;
    my $cgi = $self->{'cgi'};
    my $header_str = $cgi->header(@_);

移位
将数组的第一个值移开并 returns 它,将数组缩短 1 并将所有内容向下移动。

http://perldoc.perl.org/functions/shift.html

这更像是现代 perl 代码的样子:

DebugCGI.pm:

package DebugCGI;

use strict;
use warnings;
use 5.016;
use Data::Dumper;

use CGI; 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


sub new {
    my ($class, $PARAMFILE) = @_;

    my $fname = 'process-cgi.txt';

    open my $OUTFILE, '>', $fname
        or die "Couldn't read from $fname: $!";

    my $obj_attributes = {
        'cgi_obj' => CGI->new($PARAMFILE),
        'outfile' => $OUTFILE,
        'header_count' => 0,
    };

    return bless $obj_attributes, $class;

}

sub header {
    my ($self, @headers) = @_;

    my $cgi = $self->{'cgi_obj'};
    my $header_str = $cgi->header(@headers);
    print {$self->{outfile}} $header_str;

    $self->{'header_count'}++;

    return;
}

sub close {
    my ($self) = @_;

    my $count = $self->{'header_count'};
    my $OUTFILE = $self->{'outfile'};

    say {$OUTFILE} "number of headers: $count";
    close $OUTFILE;

    return;
}

1;

测试一下:

use strict;
use warnings;
use 5.016;
use Data::Dumper;

use DebugCGI;


my $fname = 'params.txt';

open my $PARAMFILE, '<', $fname
    or die "Couldn't open $fname: $!";

my $debug_cgi = DebugCGI->new($PARAMFILE);

close $PARAMFILE;

$debug_cgi->header(
    '-type' => 'text/html; charset=UTF-8',
);

$debug_cgi->header(
    '-type' => 'text/plain: charset=UTF-8',
);

$debug_cgi->close;

params.txt:

x=3
y=4

输出:

$ cat process-cgi.txt
Content-Type: text/html; charset=UTF-8

Content-Type: text/plain: charset=UTF-8

number of headers: 2

请注意 $cgi->header() 在其输出后添加的双换行符。双换行符是向浏览器发出的信号,表明 header 已经结束,任何后续文本都将被视为响应的 body。因此,您不能 print $cgi->header() 两次,因为第二次文本不会被视为 header。如果出于某种原因你想 print $cgi->header() 两次,那么你可以用 s/\s+\z//xms.

去掉尾随的换行符