Welcome to the Monastery | |
PerlMonks |
Re: The unicode / utf8 struggle, part 2: regexesby graff (Chancellor) |
on May 17, 2007 at 14:20 UTC ( [id://616004]=note: print w/replies, xml ) | Need Help?? |
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: 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: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 Section
Seekers of Perl Wisdom
|
|