Note: you only get away succeed with what you did, because Portuguese uses Latin1, and because utf8::decode modifies its argument even on failure (which is surprising).
The utf8::decode expects octets, but you're trying to feed it with proper Unicode. Of course it fails (check return value), but nevertheless, for whatever vague reason, it converts the utf8 string to latin1-encoded string, -- only when, it seems, such conversion is possible.
use strict;
use warnings;
use utf8;
my $x1 = my $x2 = 'ç';
die if utf8::decode($x2);
use Devel::Peek;
Dump $x1;
Dump $x2;
>perl test170921.pl
SV = PV(0xe1af88) at 0xd23560
REFCNT = 1
FLAGS = (POK,IsCOW,pPOK,UTF8)
PV = 0xd6b458 "\303\247"\0 [UTF8 "\x{e7}"]
CUR = 2
LEN = 10
COW_REFCNT = 1
SV = PV(0xe1af58) at 0xd22fc0
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0xd6b5a8 "\347"\0
CUR = 1
LEN = 10
So this encoding from utf8 to latin1 (even though you did it inadvertently with call to decode) is just a special narrow case of what I wrote in this thread about encoding to your system codepage when reaching outside from Perl, and decoding whatever you fetch back.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.