Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You're confusing the internal representation (as reported by is_utf8) and the external one.

+-----------------------------------------------------------------+ | | | Decoded Text | | | | | | +--------------------+ downgrade +--------------------+ | | | Internally encoded | --------------> | Internally encoded | | | | as UTF-8 | | as iso-8859-1 | | | | (is_utf8 = 1) | <-------------- | (is_utf8 = 0) | | | +--------------------+ upgrade +--------------------+ | | | +-----------------------------------------------------------------+ | ^ | | encode | | decode | | v | +-----------------------------------------------------------------+ | | | Bytes or | | Encoded Text | | | | | | +--------------------+ downgrade +--------------------+ | | | Internally encoded | --------------> | Internally encoded | | | | as UTF-8 | | as iso-8859-1 | | | | (is_utf8 = 1) | <-------------- | (is_utf8 = 0) | | | +--------------------+ upgrade +--------------------+ | | | +-----------------------------------------------------------------+

  • upgrade refers to utf8::upgrade or an implicit upgrade.
  • downgrade refers to utf8::downgrade.
  • decode refers to Encode::decode, utf8::decode, :encoding on an input stream, etc.
  • encode refers to Encode::encode, utf8::encode, :encoding on an output stream, etc.
  • is_utf8 refers to Encode::is_utf8 or utf8::is_utf8 (which return the value of the UTF8 flag).

  • utf8::upgrade is safe to call on strings that are already upgraded.
  • utf8::downgrade is safe to call on strings that are already downgraded.
  • It is a bug to encode a string that's already encoded.
  • It is a bug to decode a string that's already decoded.

Your code should be

use Encode qw(is_utf8 encode decode); binmode STDOUT,':encoding(iso-8859-1)'; my $str = "This's a \x{201c}test\x{201d}"; # This is a "decoded" str. print "$utf8\n"; # Encoded by :encoding
use Encode qw(is_utf8 encode decode); my $str = "This's a \x{201c}test\x{201d}"; # This is a "decoded" str. print encode('iso-8859-1', "$utf8\n"); # Include the LF.

Why, perl say that it's an utf8 but can't decode it?

Perl said the internal encoding is UTF8. You shouldn't have care what the internal encoding is. (Unfortunately, you still need to know in some circumstances. This isn't one of those.)

if \x{201c} is not an utf8 character

There's no such thing as a "utf8 character" or "UTF-8 character" since utf8 and UTF-8 aren't character sets. \x{201c} generates a Unicode character (U+201C, LEFT DOUBLE QUOTATION MARK) which can be encoded using UTF-8.

In reply to Re: Decoding, Encoding string, how to? (internal encoding) by ikegami
in thread Decoding, Encoding string, how to? by way

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [Lady_Aleena]: I might have to go with options instead of a straight list.
    [Discipulus]: if more than 3 go for named variables LA
    [Lady_Aleena]: Discipulus, it is at 2 now, but with what I am thinking about, it could go to 3. However, only 1 is needed. The second and third are optional.
    [shmem]: straight list or named parameters - that depends on whether (and how many) optional arguments you have
    [Discipulus]: if so a plain list is ok, imho
    [Lady_Aleena]: shmem, I already have the plugin installed, just not active.
    [shmem]: if you have 1 optional argument, place that at the end of the list. If you have more, go for named parameters.
    [Lady_Aleena]: 1 manditory, 2 optional.
    [Discipulus]: my ($need,$opta,$optb ) = @_; .. if $opta..
    [shmem]: all else leads to trouble, even if the third argument depends on the existence of the second. That may become brittle.

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (12)
    As of 2017-04-27 12:19 GMT
    Find Nodes?
      Voting Booth?
      I'm a fool:

      Results (506 votes). Check out past polls.