It took a little editing but I was able to reproduce this (Linux / Perl 5.26). Note that AFAIK PerlMonks defaults to the Latin1 character set encoding, so as opposed to the file you get from the [download] link, I am assuming that your source code file is correctly encoded in valid UTF-8 and contains the correctly encoded characters "É" and "é". In my post below I am using the <pre> instead of <code> tags (and a few other replacements) in order to get PerlMonks to display the post correctly. For posting on PerlMonks, personally I would not use utf8; and use \x{00C9} escapes instead, although then again, if I do that with your code, none of the input strings are flagged as UTF-8. (Update: With \N{U+00C9}, they do get flagged as UTF8.)
It appears that HTML::Entities's decode_entities does not enable the UTF8 flag on the string, so that the non-UTF8 string "Édition limitée." does not get upgraded.
#!/usr/bin/env perl
use warnings;
use strict;
use utf8;
use open qw/:std :utf8/;
use HTML::Entities;
use Devel::Peek;
my @strs = ("Édition limitÉe.",
"Édition limitÉe.",
"Édition limitÉe.");
for my $str (@strs) {
$str = decode_entities($str);
Dump($str);
$str = lc($str);
Dump($str);
print "{$str}\n";
}
__END__
# Output edited for brevity
SV = ... FLAGS = (POK,pPOK,UTF8)
PV = 0xf667c0 "\303\211dition limit\303\211e."\0 [UTF8 "\x{c9}dition limit\x{c9}e."]
SV = ... FLAGS = (POK,pPOK,UTF8)
PV = 0xf667c0 "\303\251dition limit\303\251e."\0 [UTF8 "\x{e9}dition limit\x{e9}e."]
{édition limitée.}
SV = ... FLAGS = (POK,pPOK,UTF8)
PV = 0xffde20 "\303\211dition limit\303\211e."\0 [UTF8 "\x{c9}dition limit\x{c9}e."]
SV = ... FLAGS = (POK,pPOK,UTF8)
PV = 0xffde20 "\303\251dition limit\303\251e."\0 [UTF8 "\x{e9}dition limit\x{e9}e."]
{édition limitée.}
SV = ... FLAGS = (POK,pPOK)
PV = 0xfe3440 "\311dition limit\311e."\0
SV = ... FLAGS = (POK,pPOK)
PV = 0xfe3440 "\311dition limit\311e."\0
{Édition limitÉe.}
The "correct" way to solve this depends a bit on where these strings you are getting are coming from. Are they all embedded in your source code? Are you reading them from a file? If so, are you opening the files with the correct layers, that is for exaple, open my $fh, '<:encoding(UTF-8)', ...?
Update: In the code, changed all é to É and é to É to make the effects more clear. (Also, I can't reproduce the dependence on the -w switch that 1nickt reported.)
In reply to Re: Unexpected interaction between decode_entities() and lc()
by haukex
in thread Unexpected interaction between decode_entities() and lc()
by kurisuto
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |