如何在Perl中缓存和清除输出缓冲区?

时间:2013-01-22 04:55:59

标签: php perl syntax porting output-buffering

我有以下PHP代码打开输出缓冲区,包含一个文件,将其存储在变量中,并清除缓冲区:

ob_start(); 
include('test.html');
$input=ob_get_clean(); 

Perl中的等效内容如何?

3 个答案:

答案 0 :(得分:4)

$| = 1;将为当前选定的句柄(默认为STDOUT)启用禁用缓冲。换句话说,

$| = 1;

在功能上等同于

use IO::Handle qw( );   # Not needed since 5.14.
select()->autoflush(1);

通常意味着

use IO::Handle qw( );   # Not needed since 5.14.
STDOUT->autoflush(1);

答案 1 :(得分:3)

特殊变量$|。设置为非零时,每次写入或打印后都会刷新缓冲区

答案 2 :(得分:2)

所以相应的是:

# open a file handle try to get test.html
open(my $fh, "<", "test.html") ||
   die 'Could not open test.html: '.$!;
# return the currently selected filehandle
select($fh);
#clear the output buffer
select()->autoflush(1);

<强>参考