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


How do I cache and clear the output buffer in Perl?

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

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

在Perl中是怎样的?

$| = 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);

特殊变量$|。当设置为非零时,在每次写入或打印

后进行缓冲区刷新

所以等价的是:

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

引用

  • perldoc:打开

  • perldoc:关键字

  • IO::文件

  • 我如何刷新/取消缓冲输出文件句柄?为什么我必须这样做?

  • 刷新输出