1
我不能確切地總結我的周圍TIE頭,只是還沒有,但(example-1example-2example-3)我見過的例子至今使用非Moosy實現,反正有做到這一點:如何使用Tie實現Moose實現來修改PRINT函數的輸出?
package MY_STDOUT;
use strict;
my $c = 0;
my $malformed_header = 0;
open(TRUE_STDOUT, '>', '/dev/stdout');
tie *STDOUT, __PACKAGE__, (*STDOUT);
sub TIEHANDLE {
my $class = shift;
my $handles = [@_];
bless $handles, $class;
return $handles;
}
sub PRINT {
my $class = shift;
if (!$c++ && @_[0] !~ /^content-type/) {
my (undef, $file, $line) = caller;
print STDERR "Missing content-type in $file at line $line!!\n";
$malformed_header = 1;
}
return 0 if ($malformed_header);
return print TRUE_STDOUT @_;
}
1;
use MY_STDOUT;
print "content-type: text/html\n\n"; #try commenting out this line
print "<html>\n";
print "</html>\n";
在更多Perl-Moosy的方式?
比如我應該做的
open(TRUE_STDOUT, '>', '/dev/stdout');
tie *STDOUT, __PACKAGE__, (*STDOUT);
在BUILD {}功能?
將它作爲Moosy類或Moose :: Role來實現會更有意義嗎?
最後,我不得不做一些像
my $MY_STDOUT = MY_STDOUT->new();
使用它?
要回答這個問題,您沒有機會在別處回答,我絕不會讓某人誤解某些東西。 – ikegami