The answer depends on how you want to use the title. There are 3 basic ways to go:
Bytes that represent an UTF-8 encoded string.
This is the format that should be used if you want to store the UTF-8 encoded string outside your application, be it on disk or sending it over the network or anything outside the scope of your program.
A string of Unicode characters.
The concept of characters is internal to perl. When you perform Encode::decode_utf8
, then a bunch of bytes is attempted to be converted to a string of characters, as seen by perl. The perl VM (and the programmer writing Perl code) cannot externalize that concept except through decoding UTF-8 bytes on input and encoding them to UTF-8 bytes on output. For example, your program receives two bytes as input that you know they represent UTF-8 encoded character(s), let's say 0xC3 0xB6
. In that case decode_utf8
returns a representation that instead of two bytes, sees one character: ö
.
You can then proceeed to manipulate that string in Perl. To illustrate the difference further, consider the following code:
my $bytes = "\xC3\xB6";
say length($bytes); # prints "2"
my $string = decode_utf8($bytes);
say length($string); # prints "1"
The special case of US-ASCII, a subset of UTF-8.
US-ASCII is a very small subset of Unicode, where characters in that range are represented by a single byte. Converting Unicode into ASCII is an inherently lossy operation, as most of the Unicode characters are not ASCII characters. You're either forced to drop every character in your string which is not in ASCII or try to map from a Unicode character to their closest ASCII equivalents (which isn't possible in the vast majority of cases), when trying to coerce a Unicode string to ASCII.
Since you have wide character warnings, it means that you're trying to manipulate (possibly output) Unicode characters that cannot be represented as US-ASCII or ISO-8859-1.
If you do not need to manipulate the title from your xml document as a string, I'd suggest you leave it as UTF-8 bytes (I'd mention that you should be careful not to mix bytes and characters in strings). If you do need to manipulate it, then decode, manipulate and on output encode it in UTF-8.
For further reading, please use perldoc
to study perlunitut, perlunifaq, perlunicode, perluniintro, Encode
.