Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Mini-Tutorial: Scoped changes to PerlIO layers

by ikegami (Pope)
on Jul 09, 2009 at 17:27 UTC ( #778633=perlmeditation: print w/replies, xml ) Need Help??

If you want to temporarily change the PerlIO layers of a handle, your best bet might be to dup the handle.
use strict; use warnings; use PerlIO qw( ); sub dump_layers($*) { my ($prefix, $fh) = @_; my @layers = PerlIO::get_layers($fh); print($prefix, join(', ', @layers), "\n"); } dump_layers('before: ', STDERR); { open my $fh, '>&', STDERR or die $!; # Dup the handle binmode $fh, ':encoding(UTF-8)'; # Do anything to the dup. dump_layers('local: ', $fh); } dump_layers('after: ', STDERR); # The original is unaffected.
before: unix, perlio local: unix, perlio, encoding(utf-8-strict), utf8 after: unix, perlio

The package variable itself could be replaced with the dup for the duration of the inner scope, but it shouldn't normally be needed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://778633]
Approved by Corion
Front-paged by Arunbear
[ambrus]: Corion: does building airports "somewhat outside of" a city ever work? cities always grow around the airport quickly, because it's practical to be close to it, and after that, people start to complain that the airplanes land so close to their house and sh
[ambrus]: ould keep down the noise

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-02-23 12:06 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (346 votes). Check out past polls.