Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Mini-Tutorial: Scoped changes to PerlIO layers

by ikegami (Patriarch)
on Jul 09, 2009 at 17:27 UTC ( [id://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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://778633]
Approved by Corion
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found