Technically, it's true that "perl's internal character format" is not exactly utf8, but the difference applies only to the unicode characters in the range U+0080 - U+00FF, which are supposed to be two-byte wide characters in "official" utf8, but are stored internally by Perl as single bytes. But you should not have to concern yourself with this technical detail.

As far as most Perl programmers are concerned, perl's internal character format is utf8, and when you have to deal with input or output data that is not utf8, then everything else you need is provided by PerlIO and/or Encode.

This is my procedure pipeline:
  • read a string from variously encoded sources --> decode it properly to get "perl's internal format"
  • do various things with the textual data
  • re-encode it to utf8 (effectively a transport/storage format) and write it to disk (in binmode).

You got the first step right -- no problem there (but go ahead and use the term "utf8" in place of "perl's internal format" -- that's true enough).

The second step is fine, assuming "things" include any of the character-based functions (index, substr, length, split, regex matches, s///, tr///, and so on). It's all done with characters, and you just need to think about characters (not bytes). Something like  s/(\d{2})\D(\d{2})\D(\d{4})/$3-$1-$2/g will rearrange digit characters, no matter whether they are ASCII, Arabic, Chinese, Devanagari or other digits.

The third step is a misunderstanding. If you have successfully "decode"d non-unicode input data to perl's utf8, and you have set your output file handle to use utf8 protocol, the output will be valid utf8 character data (1 to 4 bytes per character, depending on which code points are involved).

The correct perl syntax for a literal unicode character code point is:  "\x{hhhh}"; you can safely use that for all code points:  print "\x{0030}\n"; will print the ASCII ZERO character followed by linefeed. (But for unicode characters below U+0100, you can also use just  "\xhh".)

UPDATE: Sorry, I should have noticed a few other things in your post...

had the following regex:
$internal_format_string =~ s/\n//g;
and it removed some letters, spaces and a lot more!

I'll bet that if you deleted carriage returns as well ( s/[\r\n]+//g), things would look better. I think there may be some "uncharted territory" involving interactions among PerlIO layers -- when you set an encoding mode on a file handle, it might affect the choice of CRLF vs. "raw" (or LF) mode in some unexpected way. You may want to study PerlIO on that issue.


In reply to Re: The unicode / utf8 struggle, part 2: regexes by graff
in thread The unicode / utf8 struggle, part 2: regexes by isync

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.