Skip to content

Instantly share code, notes, and snippets.

@DRVTiny
Last active February 28, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRVTiny/3758c139f95e16c62749c63896b66e14 to your computer and use it in GitHub Desktop.
Save DRVTiny/3758c139f95e16c62749c63896b66e14 to your computer and use it in GitHub Desktop.
# To avoid IO::Handle and other IO::* "autoloading" when using $fh->autoflush(1)
sub __autoflush($) {
my $fl_have2select = defined(my $fh = $_[0]);
# previously selected file handler
my $prv_sel_fh;
if ( $fl_have2select and ! eval {
no warnings;
$prv_sel_fh = select $fh;
print '' or die "$!\n";
}) {
# __out_to_tty '>>', __chomp(\$@), '<<';
defined($prv_sel_fh) and select($prv_sel_fh);
return
}
my $prv_af = $|;
my $new_af = ($#_ > 0) && ! $_[1] ? 0 : 1;
( !! $prv_af ^ !! $new_af ) and $| = $new_af;
$fl_have2select and fileno($prv_sel_fh) != fileno($fh) and select($prv_sel_fh);
return $prv_af
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment