The special variable $|
. When set to nonzero, does a buffer flush after every write or print
How do I cache and clear the output buffer in Perl?
-
17-01-2022 - |
Question
I have the following PHP code which opens the output buffer, includes a file, stores it in a variable, and clears the buffer:
ob_start();
include('test.html');
$input=ob_get_clean();
How would the equivalent look in Perl?
Solution 2
OTHER TIPS
$| = 1;
will turn on disable buffering for the currently selected handle (STDOUT
by default). In other words,
$| = 1;
is functionally equivalent to
use IO::Handle qw( ); # Not needed since 5.14.
select()->autoflush(1);
which usually means
use IO::Handle qw( ); # Not needed since 5.14.
STDOUT->autoflush(1);
So the equivalent would be:
# 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);
References
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow