Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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 "$str\n"; # Encoded by :encoding
or
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', "$str\n");

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

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-23 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found