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 :-)
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ