开发者

charnames, substitutions and Umlaut "Ü": Malformed UTF-8 character

I´m working with bibliographic records which uses control characters for text seperation. When I use the Unicode character name "STRING TERMIN开发者_StackOverflowATOR" within substitute operations I get an "Malformed UTF-8 character" warning if the text contains an "Ü". The substitution works with other Unicode character names and text containing "ü", "ä", "é",...

#!/usr/bin/perl

use v5.12;

use utf8;
use strict;
use autodie;
use warnings;
# use warnings    qw< FATAL  utf8     >;
use charnames   qw< :full >;
use feature     qw< unicode_strings >;

binmode STDOUT, ':utf8';

my @records = (qq[\N{U+0098}L'\N{U+009c} Année du Figaro], qq[\N{U+0098}The\N{U+009c} famous ümläut], qq[\N{U+0098}The\N{U+009c} famous Ümlaut], qq[\N{U+0098}\N{U+00DC}\N{U+009c}]);

my %replace = (
    "\N{START OF STRING}"     => "<ns>",
    "\N{STRING TERMINATOR}"   => "</ns>",
);
my $regex = join "|", keys %replace;
$regex = qr/$regex/;

foreach my $record (@records){
    $record =~ s/($regex)/$replace{$1}/g;
    say $record;
};

I use Strawberry Perl v5.12.3.0.

What can do to avoid these warnings?

Thanks, jorol


Upgrade to 5.14, it won't throw warnings there.


If possible you can have a list of all Unicode characters in a hash and substitute it before processing, in order to avoid warnings for the Perl Version you have

Something like this,

my $Description = "famous Ümlaut";
my %UMLAUTE = ( 
    'Ä' => 'Ae',
    'Ö' => 'Oe', 
    'Ü' => 'Ue',
    'ä' => 'ae',
    'ö' => 'oe', 
    'ü' => 'ue', 
    'ß' => 'ss',
    'é' => 'e' 
);

my @UMLKEYS = join("|", keys(%UMLAUTE));
$Description =~ s/(@UMLKEYS)/$UMLAUTE{$1}/g; 

This would result in "famous Uemlaut"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜