簡體   English   中英

perl:無法在對象中打印到文件句柄?

[英]perl: Can't print to filehandle in object?

我正在嘗試調試不報告任何錯誤的cgi腳本,但瀏覽器顯示生成的文本,而不是呈現頁面。 我從某種容器對象中調用了cgi,以查看是否兩次發送了標頭。

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());

哦。

但除了“標頭數:0”以外,沒有任何內容打印到文件上,並且仍然可以顯示完整的HTML文檔。 我做錯了什么,該如何改善?

您在這里遇到問題:

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

$ self是您的哈希,其中包含cgi對象。 因此,您需要這樣做:

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

轉移
將數組的第一個值移開並返回,將數組縮短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()在輸出之后添加的雙換行符。 雙換行符是向瀏覽器發出的信號,表明標頭已結束,並且任何后續文本均應視為響應的正文。 因此,您不能兩次print $cgi->header() ,因為第二次該文本將不被視為標題。 如果出於某種原因要print $cgi->header()兩次,則可以使用s/\\s+\\z//xms尾隨的換行符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM