Unicode::Japanese(3pm) | User Contributed Perl Documentation | Unicode::Japanese(3pm) |
Unicode::Japanese - Convert encoding of japanese text
use Unicode::Japanese; use Unicode::Japanese qw(unijp); # convert utf8 -> sjis print Unicode::Japanese->new($str)->sjis; print unijp($str)->sjis; # same as above. # convert sjis -> utf8 print Unicode::Japanese->new($str,'sjis')->get; # convert sjis (imode_EMOJI) -> utf8 print Unicode::Japanese->new($str,'sjis-imode')->get; # convert zenkaku (utf8) -> hankaku (utf8) print Unicode::Japanese->new($str)->z2h->get;
The Unicode::Japanese module converts encoding of japanese text from one encoding to another.
Currently the method get() returns UTF-8 byte string but this behavior may be changed in the future.
Methods like sjis(), jis(), utf8(), and such like return byte string. new(), set(), getcode() methods just ignore the UTF-8 flag of strings they take.
No other modules are required at run time.
Any given parameters will be internally passed to the method "set"().
Store a string into the instance.
Possible character encodings are:
auto utf8 ucs2 ucs4 utf16-be utf16-le utf16 utf32-be utf32-le utf32 sjis cp932 euc euc-jp jis sjis-imode sjis-imode1 sjis-imode2 utf8-imode utf8-imode1 utf8-imode2 sjis-doti sjis-doti1 sjis-jsky sjis-jsky1 sjis-jsky2 jis-jsky jis-jsky1 jis-jsky2 utf8-jsky utf8-jsky1 utf8-jsky2 sjis-au sjis-au1 sjis-au2 jis-au jis-au1 jis-au2 sjis-icon-au sjis-icon-au1 sjis-icon-au2 euc-icon-au euc-icon-au1 euc-icon-au2 jis-icon-au jis-icon-au1 jis-icon-au2 utf8-icon-au utf8-icon-au1 utf8-icon-au2 ascii binary
(see also "SUPPORTED ENCODINGS".)
If you want the Unicode::Japanese detect the character encoding of string, you must explicitly specify 'auto' as the second argument. In that case, the given string will be passed to the method getcode() to guess the encoding.
For binary encodings, only 'base64' is currently supported. If you specify 'base64' as the third argument, the given string will be decoded using Base64 decoder.
Specify 'binary' as the second argument if you want your string to be stored without modification.
When you specify 'sjis-imode' or 'sjis-doti' as the character encoding, any occurences of '&#dddd;' (decimal character reference) in the string will be interpreted and decoded as code point of emoji, just like emoji implanted into the string in binary form.
Since encoded forms of strings in various encodings are not clearly distinctive to each other, it is not always certainly possible to detect what encoding is used for a given string.
When a given string is possibly interpreted as both Shift_JIS and UTF-8 string, this module considers such a string to be encoded in Shift_JIS. And if the encoding is not distinguishable between 'sjis-au' and 'sjis-doti', this module considers it 'sjis-au'.
Get the internal string in UTF-8.
This method currently returns a byte string (whose UTF-8 flag is turned off), but this behavior may be changed in the future.
If you absolutely want a byte string, you should use the method utf8() instead. And if you want a character string (whose UTF-8 flag is turned on), you have to use the method getu().
Get the internal string in UTF-8.
On perl-5.8.0 or later, this method returns a character string with its UTF-8 flag turned on.
Detect the character encoding of given string.
Note that this method, exceptionaly, doesn't deal with the internal string of an instance.
To guess the encoding, the following algorithm is used:
(For pure perl implementation)
(For XS implementation)
ascii / euc / sjis / jis / utf8 / utf32-be / utf32-le / sjis-jsky / sjis-imode / sjis-au / sjis-doti
utf32-be / utf32-le / ascii / jis / euc / sjis / sjis-jsky / sjis-imode / sjis-au / sjis-doti / utf8
Pay attention to the following pitfalls in the above algorithm:
Since the XS and pure perl implementations use different algorithms to guess encoding, they may guess differently for the same string. Especially, the pure perl implementation finds Shift_JIS strings containing ESC character (0x1B) to be actually encoded in Shift_JIS but XS implementation doesn't. This is because such strings can hardly be distinguished from 'sjis-jsky'. In addition, EUC-JP strings containing ESC character are also rejected for the same reason.
Detect the character encoding of given string.
Unlike the method getcode(), getcodelist() returns a list of possible encodings.
utf8 ucs2 ucs4 utf16 sjis cp932 euc euc-jp jis sjis-imode sjis-imode1 sjis-imode2 utf8-imode utf8-imode1 utf8-imode2 sjis-doti sjis-doti1 sjis-jsky sjis-jsky1 sjis-jsky2 jis-jsky jis-jsky1 jis-jsky2 utf8-jsky utf8-jsky1 utf8-jsky2 sjis-au sjis-au1 sjis-au2 jis-au jis-au1 jis-au2 sjis-icon-au sjis-icon-au1 sjis-icon-au2 euc-icon-au euc-icon-au1 euc-icon-au2 jis-icon-au jis-icon-au1 jis-icon-au2 utf8-icon-au utf8-icon-au1 utf8-icon-au2 binary
(see also "SUPPORTED ENCODINGS".)
Some encodings for mobile phones have a trailing digit like 'sjis-au2'. Those digits represent the version number of encodings. Such encodings have a variant with no trailing digits, like 'sjis-au', which is the same as the latest version among its variants.
Get the internal string of instance with encoding it using a given character encoding method.
If you want the resulting string to be encoded in Base64, specify 'base64' as the second argument.
On perl-5.8.0 or later, the UTF-8 flag of resulting string is turned off even if you specify 'utf8' to the first argument.
Get the internal string of instance with encoding it in ISO-2022-JP.
Get the internal string of instance with encoding it in EUC-JP.
Get the internal UTF-8 string of instance.
On perl-5.8.0 or later, the UTF-8 flag of resulting string is turned off.
Get the internal string of instance as a sequence of raw UCS-2 letters in big-endian. Note that this is different from UTF-16BE as raw UCS-2 sequence has no concept of surrogate pair.
Get the internal string of instance as a sequence of raw UCS-4 letters in big-endian. This is practically the same as UTF-32BE.
Get the insternal string of instance with encoding it in UTF-16 in big-endian with no BOM prepended.
Get the internal string of instance with encoding it in Shift_JIS (MS-SJIS / MS-CP932).
Get the internal string of instance with encoding it in 'sjis-imode'.
Get the internal string of instance with encoding it in 'sjis-imode1'.
Get the internal string of instance with encoding it in 'sjis-imode2'.
Get the internal string of instance with encoding it in 'sjis-doti'.
Get the internal string of instance with encoding it in 'sjis-jsky'.
Get the internal string of instance with encoding it in 'sjis-jsky1'.
Get the internal string of instance with encoding it in 'sjis-jsky'.
Get the internal string of instance with encoding it in 'sjis-icon-au'.
Split the internal string of instance into chunks of a given length.
On perl-5.8.0 or later, UTF-8 flags of each chunks are turned on.
Calculate the character width of the internal string. Half-width characters have width of one unit, and full-width characters have width of two units.
Build a line of CSV from the arguments, and store it into the instance. The resulting line has a trailing line break ("\n").
Parse a line of CSV in the instance and return each columns. The line will be chomp()ed before getting parsed.
If the internal string was decoded from 'binary' encoding (see methods new() and set()), the UTF-8 flags of the resulting array of strings are turned off. Otherwise the flags are turned on.
+---------------+----+-----+-------+ |encoding | in | out | guess | +---------------+----+-----+-------+ |auto : OK : -- | ----- | +---------------+----+-----+-------+ |utf8 : OK : OK | OK | |ucs2 : OK : OK | ----- | |ucs4 : OK : OK | ----- | |utf16-be : OK : -- | ----- | |utf16-le : OK : -- | ----- | |utf16 : OK : OK | OK(#) | |utf32-be : OK : -- | OK | |utf32-le : OK : -- | OK | |utf32 : OK : -- | OK(#) | +---------------+----+-----+-------+ |sjis : OK : OK | OK | |cp932 : OK : OK | ----- | |euc : OK : OK | OK | |euc-jp : OK : OK | ----- | |jis : OK : OK | OK | +---------------+----+-----+-------+ |sjis-imode : OK : OK | OK | |sjis-imode1 : OK : OK | ----- | |sjis-imode2 : OK : OK | ----- | |utf8-imode : OK : OK | ----- | |utf8-imode1 : OK : OK | ----- | |utf8-imode2 : OK : OK | ----- | +---------------+----+-----+-------+ |sjis-doti : OK : OK | OK | |sjis-doti1 : OK : OK | ----- | +---------------+----+-----+-------+ |sjis-jsky : OK : OK | OK | |sjis-jsky1 : OK : OK | ----- | |sjis-jsky2 : OK : OK | ----- | |jis-jsky : OK : OK | ----- | |jis-jsky1 : OK : OK | ----- | |jis-jsky2 : OK : OK | ----- | |utf8-jsky : OK : OK | ----- | |utf8-jsky1 : OK : OK | ----- | |utf8-jsky2 : OK : OK | ----- | +---------------+----+-----+-------+ |sjis-au : OK : OK | OK | |sjis-au1 : OK : OK | ----- | |sjis-au2 : OK : OK | ----- | |jis-au : OK : OK | ----- | |jis-au1 : OK : OK | ----- | |jis-au2 : OK : OK | ----- | |sjis-icon-au : OK : OK | ----- | |sjis-icon-au1 : OK : OK | ----- | |sjis-icon-au2 : OK : OK | ----- | |euc-icon-au : OK : OK | ----- | |euc-icon-au1 : OK : OK | ----- | |euc-icon-au2 : OK : OK | ----- | |jis-icon-au : OK : OK | ----- | |jis-icon-au1 : OK : OK | ----- | |jis-icon-au2 : OK : OK | ----- | |utf8-icon-au : OK : OK | ----- | |utf8-icon-au1 : OK : OK | ----- | |utf8-icon-au2 : OK : OK | ----- | +---------------+----+-----+-------+ |ascii : OK : -- | OK | |binary : OK : OK | ----- | +---------------+----+-----+-------+ (#): guessed when it has bom.
1. utf32 (#) 2. utf16 (#) 3. utf32-be 4. utf32-le 5. ascii 6. jis 7. sjis-jsky (pp) 8. euc 9. sjis 10. sjis-jsky (xs) 11. sjis-au 12. sjis-imode 13. sjis-doti 14. utf8 15. unknown
Transcoding between Unicode encodings and other ones is performed as below:
<ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP932.TXT>
When the module tries to convert Unicode string to Shift_JIS, it represents most letters which isn't available in Shift_JIS as decimal character reference ('&#dddd;'). There is one exception to this: every graphic characters for mobile phones are replaced with '?' mark.
For variants of Shift_JIS defined for mobile phones, every unrepresentable characters are replaced with '?' mark unlike the plain Shift_JIS.
This module considers a pair of those first and second bytes to be one letter, and map them from 4500 - 47FF to U+0FFB00 - U+0FFDFF.
When the module encodes J-SKY emoji, it performs the compression automatically.
use Unicode::Japanese qw(PurePerl);
If you want to explicitly take the pure perl implementation, pass 'PurePerl' to the argument of the "use" statement.
Please report bugs and requests to "bug-unicode-japanese at rt.cpan.org" or <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Unicode-Japanese>. If you report them to the web interface, any progress to your report will be automatically sent back to you.
You can find documentation for this module with the perldoc command.
perldoc Unicode::Japanese
You can find more information at:
<http://annocpan.org/dist/Unicode-Japanese>
<http://cpanratings.perl.org/d/Unicode-Japanese>
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Unicode-Japanese>
<http://search.cpan.org/dist/Unicode-Japanese>
Thanks very much to:
NAKAYAMA Nao
SUGIURA Tatsuki & Debian JP Project
Copyright 2001-2008 SANO Taku (SAWATARI Mikage) and YAMASHINA Hio, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2018-11-01 | perl v5.28.0 |