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

Re: Restrict encoding of std streams to lexical scope

by Perlbotics (Archbishop)
on Aug 09, 2013 at 12:43 UTC ( [id://1048762]=note: print w/replies, xml ) Need Help??


in reply to Restrict encoding of std streams to lexical scope

I fiddled around with require and open::import(...) but that didn't made things better. Since, you want a lexical-region where STDOUT has UTF-properties, you could emulate that with a prototyped helper function that employed binmode and local to temporarily change STDOUT's behaviour:

use warnings; use v5.10; use utf8; #-- prototype to setup a block where STDOUT is UTF-8 sub with_utf(&) { open(my $stdout, ">&STDOUT"); #-- STDERR analogue local *STDOUT = $stdout; binmode(*STDOUT, ':encoding(UTF-8)'); shift->(); } my $utf8 = "\x{3bc}\x{1fc6}\x{3bd}\x{3b9}\x{3bd}"; say "Default: $utf8"; # Wide character in say at 1048755.pl line 16 +. with_utf { say "Lexical: $utf8"; # (no warning) }; say "Default: $utf8"; # Wide character in say at 1048755.pl line 22 +.

Result:

linux> perl 1048755.pl
Wide character in say at 1048755.pl line 16.
Default: μῆνιν
Lexical: μῆνιν
Wide character in say at 1048755.pl line 22.
Default: μῆνιν

Seems, choroba had the same idea... ;-)

Replies are listed 'Best First'.
Re^2: Restrict encoding of std streams to lexical scope
by choroba (Cardinal) on Aug 09, 2013 at 12:53 UTC
    I even played with require and import in the beginning, as well, to no avail :-)
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1048762]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found