perl-unicode

Re: Determining IO layer set on filehandle

2010-01-29 12:52:08
* Michael Ludwig <michael(_dot_)ludwig(_at_)xing(_dot_)com> [2010-01-29 18:30]:
It appears you can use that information to restore a filehandle
configuration:

# Gut: STDOUT duplizieren und Duplikat umstellen.
# STDOUT (global) wird nicht verstellt.
sub out_bin_good {
   open my $fh, '>&STDOUT' or die "dup STDOUT: $!";
   binmode $fh, ':raw' or die "binmode: $!";
   print $fh "BINÄR 3\t", @_;
   print STDERR "* layer: $_\n" for PerlIO::get_layers( $fh );
}

# Auch gut: IO-Modus sichern und wiederherstellen.
sub out_bin_also_good {
   my @layers = PerlIO::get_layers( STDOUT );
   binmode STDOUT, ':raw' or die "binmode: $!";
   print "BINÄR 4\t", @_;
   print STDERR "* layer: $_\n" for PerlIO::get_layers( STDOUT );
   my $layers = join '', map ":$_", @layers;
   binmode STDOUT, $layers;
   print STDERR "reset STDOUT to $layers\n";
   print STDERR "* layer: $_\n" for PerlIO::get_layers( STDOUT );
}

Considering the relative complexities of the approaches and the
fact that conservation of filehandle state is not a concern in
your case, I know which solution *I* would favour…

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>