Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
[LanX]: ah... Denmark is planning to build a wall at the German border!
[LanX]: # Pestis Africana Suum
[erix]: that's an old plan, I think. Isn't there already a danewal (or some such name)?
[marto]: Build with -Wall
[erix]: Danewerk
[LanX]: I seem to remember that the danewall was build by the Frankish empire against the Danes
[erix]: 500 AD thru viking age
[erix]: that would make sense but wikipedia thinks otherwise
[LanX]: oh me was wrong ...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2018-03-22 18:38 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (283 votes). Check out past polls.