develooper Front page | perl.unicode | Postings from September 2010

utf8 pragma, lexical scope

Thread Next
From:
Michael Ludwig
Date:
September 9, 2010 13:01
Subject:
utf8 pragma, lexical scope
Message ID:
20100909200048.GA2556@wladimir
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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About