Your understanding of Encode is correct, your input of the original string is the issue. The expression 'Köln' will produce bytes in whatever encoding your script is in, not a decoded string. There are several ways to fix this:

1. Tell perl that all of your hard-coded strings are INPUT as utf8 (Note: I'm certain that your editor is set up for UTF-8 input from the output you received, but you should convince yourself of that too and then learn how to configure it)

use utf8; # All hard-coded strings will be assumed to be UTF-8 my $temp = encode( "iso-8859-1", 'Köln' ); ...

2. Tell perl that this one string was input as utf8 (again, it is UTF-8 because that is what your editor produces)

my $temp = encode( "iso-8859-1", decode("UTF-8", 'Köln') ); ...

The second case most closely resembles what happens when you process a file or command-line arguments:

# Files (change input encoding to match file encoding): open my $F, "<:encoding(UTF-8)", "myfile" or die "Error reading myfile +: $!"; my $line = <$F>; # $line contains a decoded string say encode( "iso-8859-1", $line ); # Command-Line args: my $arg = decode("UTF-8", $ARGV[0]); # Or, command-line args is an appropriate use of Encode::Locale use Encode::Locale; my $arg = decode("locale", $ARGV[0]);

Your output of "Köln(5)" tells us that your editor and your terminal are in UTF-8 encoding and $temp is double-encoded mojibake (just much less spectacularly obvious than usual mojibake).

Just keep in mind that once you decide to care about encoding: All input must be first decoded somehow (including strings input directly into program), then it must be encoded before output. If you find odd issues with encoding, ask where it was decoded and where it was encoded (and then ask yourself whether it was decoded or encoded twice).

Good Day,
    Dean


In reply to Re: possible missunderstanding of package Encode by duelafn
in thread possible missunderstanding of package Encode by toohoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.