Jcode::Nihongo(3pm)

โœจ Jcode::Nihongo(3pm) โœจ

User Contributed Perl Documentation ๐ŸŽ€ okay bestie, this is the manpage for the Jcode Japanese charset handler ๐Ÿ’–

โœจ ๅๅ‰ โœจ

Jcode - ๆ—ฅๆœฌ่ชžๆ–‡ๅญ—้›†ๅˆใƒใƒณใƒ‰ใƒฉ ๐Ÿ“ the Japanese text-handling queen ๐Ÿ‘‘

โœจ ๆฆ‚่ฆ โœจ

use Jcode;
#
# ๅคๅผใ‚†ใ‹ใ—ใ
Jcode::convert($str, $ocode, $icode, "z");
# ใ‚‚ใ—ใใฏใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆŒ‡ๅ‘ใง๏ผ
print Jcode->new($str)->h2z->tr($from, $to)->utf8;

โœจ ่ชฌๆ˜Ž โœจ

ngl this module is so cute ๐Ÿฅน Jcode.pm supports BOTH the object-y way and the old-school way, so you can pick whatever matches your vibe ๐Ÿฆ‹ With the object approach it looks like this, literally so clean:

$iso_2022_jp = Jcode->new($str)->h2z->jis;

The old way is also giving, but honestly the one above is more elegant ๐Ÿ’…:

$iso_2022_jp = $str;
&jcode::convert(\$iso_2022_jp, 'jis', &jcode::getcode(\$str), "z");

If objects are new to you, no stress ๐Ÿซถ Jcode.pm still supports getcode() and convert(), so you're totally covered.

If your Perl is 5.8.1 or newer, Jcode acts as a wrapper around Encode, which is the standard charset handler module since Perl 5.8 โœจ iykyk.

โœจ ใƒกใ‚ฝใƒƒใƒ‰ โœจ

Unless I say otherwise, every method here returns a Jcode object ๐ŸŒธ

โœจ ใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟ โœจ

  • $j = Jcode->new($str [, $icode]) โ€” makes a Jcode object $j out of $str ๐Ÿงš If you don't explicitly set $icode, the input code gets auto-detected, so lowkey magic. For the available charsets, check out getcode further down.

On Perl 5.8.1 and up, $icode can be ANY encoding name that Encode understands ๐ŸŒˆ

$j = Jcode->new($european, 'iso-latin1');

When you stringify the object, you get back the EUC-converted string. So instead of "print $j->euc", you can just write "print $j" ๐Ÿ’œ

  • ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นๆธกใ— โ€” you can pass a reference instead of a scalar value, like this:
Jcode->new(\$str);

This saves a tiny bit of time ๐Ÿ’… The catch is that the value of $str itself gets converted (kinda like $str is "tied" to the jcode object, no cap ๐Ÿ™ˆ).

  • $j->set($str [, $icode]) โ€” sets $str as the internal string of $j ๐ŸŽ€ This is clutch when you're reusing the same Jcode object over and over, because you save both object-creation time and memory โœจ
# mailboxใ‚’SJISๅฝขๅผใซๅค‰ๆ›
my $jconv = new Jcode;
$/ = 00;
while(<>){
    print $jconv->set(\$_)->mime_decode->sjis;
}
  • $j->append($str [, $icode]); โ€” tacks $str onto the end of $j's internal string ๐Ÿฆ‹
  • $j = jcode($str [, $icode]); โ€” the short-and-sweet version of Jcode->new() ๐Ÿ“ You can do stuff like:
$sjis = jcode($str)->sjis;

โœจ ๆ–‡ๅญ—ๅˆ—ใฎใ‚จใƒณใ‚ณใƒผใƒ‰ โœจ

Generally, you get the string converted into the encoding you want with $j->encoded (fill in whatever encoding you're after) ๐Ÿ’–

  • $sjis = $j->sjis
  • $euc = $j->euc
  • $jis = $j->jis
  • $sjis = $j->sjis
  • $ucs2 = $j->ucs2
  • $utf8 = $j->utf8 โ€” honestly self-explanatory ๐Ÿ™ˆ WYCIWYG = What you code is what you get :)
  • $iso_2022_jp = $j->iso_2022_jp โ€” this is the same as "$j->h2z->jis" ๐Ÿงƒ Half-width kana are forcibly converted to full-width, no exceptions bestie.

On Perl 5.8.1 and up, you can use any encoding name and alias that Encode supports. For example:

$european = $j->iso_latin1; # ใƒกใ‚ฝใƒƒใƒ‰ๅ็”จใซ'-'ใฏ'_'ใซ็ฝฎใๆ›ใˆใพใ™

Fun fact: Encode::Encoder uses this same little trick ๐Ÿค“

  • $j->fallback($fallback) โ€” On Perl 5.8.1 and up, Jcode holds its internal string as UTF-8 ๐Ÿ˜ญ Any character that can't be mapped to the ->encoding gets swapped out for '?'. That's the standard Encode behaviour, so don't panic.
my $unistr = "\x{262f}"; # ้™ฐ้™ฝใƒžใƒผใ‚ฏ
my $j = jcode($unistr);  # $j->euc ใฎ็ตๆžœใฏ '?' ใซ

You can change this by specifying a fallback, just like in Encode ๐Ÿ’… The values are the same as the ones in Encode. For convenience, "Jcode::FB_PERLQQ", "Jcode::FB_XMLCREF" and "Jcode::FB_HTMLCREF" are aliases for their Encode counterparts โœจ

print $j->fallback(Jcode::FB_PERLQQ)->euc;   # '\x{262f}'
print $j->fallback(Jcode::FB_XMLCREF)->euc;  # '&#x262f;'
print $j->fallback(Jcode::FB_HTMLCREF)->euc; # '&#9775;'

The default fallback is stored in the global variable $Jcode::FALLBACK, so you can override it by assigning to it:

$Jcode::FALLBACK = Jcode::FB_PERLQQ; # ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒ•ใ‚ฉใƒผใƒซใƒใƒƒใ‚ฏใ‚นใ‚ญใƒผใƒžใ‚’่จญๅฎš
  • [@lines =] $jcode->jfold([$width, $newline_str, $kref]) โ€” folds the lines inside the jcode string every $width characters (default: 72) ๐ŸŒท $width counts "half-width" characters, so a full-width character counts as 2 โœจ

The newline given by $newline_str (default is "\n") gets added in. It returns all the folded lines as a list ๐Ÿ“œ

On Perl 5.8.1 and up, passing an array reference of EUC strings as the third argument $kref gives you a simple line-head kinsoku shori (the hanging-punctuation thing called ใถใ‚‰ไธ‹ใŒใ‚Š) ๐Ÿซง

  • $length = $jcode->jlength(); โ€” returns the character count, not the byte length, counting each full-width character as one character ๐Ÿ’–
๐Ÿ’€ Heads up: this is the first half of the page, so there's more juicy stuff coming next. Stay tuned, bestie ๐Ÿงš

โœจ ใ‚ตใƒ–ใƒซใƒผใƒใƒณ โœจ

($code, [$nmatch]) = getcode($str)

okay so this one is a subroutine ๐Ÿ’… It hands you back the character encoding of $str. The possible codes are:

  • ascii โ€” Ascii, with no Japanese codes in it ๐ŸŒธ
  • binary โ€” Binary, so it's not a text file ๐Ÿงฑ
  • euc โ€” EUC-JP ๐Ÿ‡ฏ๐Ÿ‡ต
  • sjis โ€” SHIFT_JIS ๐Ÿ™
  • jis โ€” JIS (ISO-2022-JP) ๐ŸŽ€
  • ucs2 โ€” UCS2 (Raw Unicode) ๐Ÿฆ‹
  • utf8 โ€” UTF8 ๐Ÿ’–

If you call it in list context instead of scalar context, you also get back how many characters' worth of codes it found. Same deal as before: $str can be \$str too, bestie โœจ

jcode.pl people, listen up ๐Ÿ‘‚ This function is 100% backwards compatible with jcode::getcode(). Well, almost 100% ๐Ÿ™ˆ

  • when the return value is a list, the order is reversed ๐Ÿ”„. jcode::getcode() gives you $nmatch first.
  • jcode::getcode() returns 'undef' when the number of EUC characters and SJIS characters is equal ๐Ÿ’€. Jcode::getcode() returns EUC in that case instead, because Jcode.pm has no middle ground ๐Ÿซ 
Jcode::convert($str, [$ocode, $icode, $opt])

This one converts $str into the character code you name with $ocode ๐Ÿ”. If you also pass $icode, it just assumes your input is that code and skips the getcode() check entirely โœจ Again, $str can be \$str ๐Ÿ’œ

jcode.pl people ๐Ÿ‘€ This function is 100% backwards compatible with jcode::convert()! No "almost" this time ๐Ÿ’…๐Ÿ”ฅ


โœจ ใƒใ‚ฐ โœจ

Heads up ๐Ÿšจ When you're on Perl 5.8.1 or newer, Jcode acts as a wrapper around Encode. That means any bugs in Encode can hit Jcode too. Not a Jcode problem, a whole-chain problem ๐Ÿ˜ญ


โœจ ่ฌ่พž โœจ

This package owes a lot to ๆญŒไปฃ ๅ’Œๆญฃ <utashiro@iij.ad.jp> ๐Ÿซถ and their Perl4 library jcode.pl, for the motivation, the design, and a big chunk of the code. Literally the foundation of everything ๐Ÿ›๏ธ

ๅคงๅดŽ ๅšๅŸบ <ohzaki@iod.ricoh.co.jp> was a huge help with polishing the regular expressions, right from the super early days ๐Ÿ’Ž

makamaka@donzoko.net and their JEncode gave me a major lightbulb moment about how to merge Jcode and Encode ๐Ÿ’ก They also donated the Japanese manual, which is so sweet ๐Ÿฅน

And the whole Jcode ML <jcode5@ring.gr.jp> crew ๐ŸŒˆ Honestly this thing would not be finished without you all ๐Ÿ’œ


โœจ ๅ‚่€ƒ โœจ


โœจ ่‘—ไฝœๆจฉ โœจ

Copyright 1999-2005 Dan Kogai <dankogai@dan.co.jp> ๐Ÿ‘‘

This library is free software ๐Ÿ•Š๏ธ You can redistribute it and/or modify it under the same terms as Perl itself. Main character energy, legally speaking ๐Ÿ’…


That's the whole page, bestie ๐ŸŽ€ Pulled from Perl v5.40.1, dated 2025-06-11, for Jcode::Nihongo(3pm). Go forth and convert those encodings, you absolute legend ๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from