perl-unicode

utf8 pragma, lexical scope

2010-09-09 15:01:07
No bug or problem, just something that works. :-) This script is in
UTF-8 (hence the utf8 pragma at the top), but it also has data in bytes,
which is taken care for by a lexical scope where utf8 is switched off.

          \,,,/
          (o o)
------oOOo-(_)-oOOo------
use strict;
use warnings;
use utf8;
# binmode STDOUT, ':utf8';
my $str1 = 'Käse'; # Käse in UTF-8, looks wrong on a latin1 terminal
my $str2;
{
  no utf8;
  $str2 = 'Käse';
  print "$_\n" for $str1, $str2;
}
print "$_\n" for $str1, $str2;
-------------------------

It works fine in 5.10.1 and 5.12.1, regardless of STDOUT setting.

What does not work, however, is to have a variable $käse under utf8
and then try to refer to it from inside a "no utf8" block, using either
encoding. Without the utf8 pragma, identifiers are not allowed to have
funny characters. (Yes, it was a stupid exercise.)
-- 
Michael Ludwig

<Prev in Thread] Current Thread [Next in Thread>