Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
[talexb]: So low end these days means quad core? Jeepers.
[jedikaiti]: 'mornin, Monks
[Corion]: talexb: Well, low end for desktops :-D
[marto]: hey jedikaiti

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-07-27 15:27 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (418 votes). Check out past polls.