Kakasi(3pm)

πŸ’– NAME πŸ’–

okay bestie, welcome to Kakasi(3pm) β€” a User Contributed Perl Documentation page, and the actual star of the show is Text::Kakasi: the perl frontend to kakasi πŸŽ€βœ¨


🌸 SYNOPSIS 🌸

use Text::Kakasi;
# functional
$res = Text::Kakasi::getopt_argv('-JJ', '-c', '-w');
$str = Text::Kakasi::do_kakasi($japanese_text);
# object-oriented
$obj = Text::Kakasi->new('-JJ', '-c', '-w');
$str = $obj->get($japanese_text);

πŸ¦‹ DESCRIPTION πŸ¦‹

So this module is literally just an interface to kakasi β€” that's kanji kana simple inverter πŸ’… kakasi is a whole set of programs and libraries that does what Japanese input methods do, but in reverse order. You feed it Japanese, and kakasi converts it into the phonetic representation of that same text. It can also tokenize Japanese text, which is lowkey iconic. Want more kakasi lore? Go look at <http://kakasi.namazu.org/> 🌐

Text::Kakasi now serves both functional and object-oriented APIs. The functional APIs are 100% compatible with ver. 1.05, no cap. But if you want to take advantage of the "Perl 5.8 Features", bestie, use the OOP APIs instead πŸ‘‘

Also: check out Text::Kakasi::JP for the Japanese version of this document πŸ‡―πŸ‡΅


✨ Functional APIs ✨

Heads up, the Text::Kakasi:: prefix is omitted below πŸ‘€ and just so you know, Text::Kakasi does not export these functions by default. You have to import them yourself, like this:

use Text::Kakasi qw/getopt_argv do_kakasi/;

🫢 $err = getopt_argv($arg1, $arg2, ...)

This initializes kakasi with options β€” and yeah, the options are literally the same as the kakasi command. Here's the summary, as of kakasi 2.3.4 πŸ—’οΈ

-a[jE] -j[aE] -g[ajE] -k[ajKH]
-E[aj] -K[ajkH] -H[ajkK] -J[ajkKH]
-i{oldjis,newjis,dec,euc,sjis}
-o{oldjis,newjis,dec,euc,sjis}
-r{hepburn,kunrei} -p -s -f -c"chars"
[jisyo1, jisyo2,,,]

Character Sets:

  • a: ascii
  • j: jisroman
  • g: graphic
  • k: kana

(j, k defined in jisx0201)

  • E: kigou
  • K: katakana
  • H: hiragana
  • J: kanji

(E, K, H, J defined in jisx0208)

Options:

  • -i β€” input coding system
  • -o β€” output coding system
  • -r β€” romaji conversion system
  • -p β€” list all readings (with -J option)
  • -s β€” insert separate characters (with -J option)
  • -f β€” furigana mode (with -J option)
  • -c β€” skip chars within jukugo (with -J option: default TAB CR LF BLANK)
  • -C β€” romaji Capitalize (with -Ja or -Jj option)
  • -U β€” romaji Upcase (with -Ja or -Jj option)
  • -u β€” call fflush() after 1 character output
  • -w β€” wakatigaki mode

Returns 0 on success and nonzero on failure βœ…

And unlike version 1.x, where you had to start the first argument with "kakasi", in version 2.x you can just omit that πŸ’« (adding "kakasi" doesn't harm anything, so the compatibility is preserved β€” everybody wins πŸŽ‰)


πŸ“ $result_str = do_kakasi($str)

Applies kakasi to $str and returns the result. And if anything goes wrong? It returns "undef" 😭


🌈 close_kanwadic()

Closes dictionary files which are implicitly opened. This one is for backward compatibility only, and fr fr you should never have to use this function today πŸ™ˆ


πŸ‘‘ Object-Oriented APIs πŸ‘‘

As of 2.0, Text::Kakasi also offers OOP APIs. slay.


πŸ’œ $k = Text::Kakasi->new($args ...)

Constructs an object ✨ When an argument is fed to it, it's the same as "Text::Kakasi->new->set($args ...)".


πŸ’… $k->set($args ...)

The OOP interface to "getopt_argv".

my $k = Text::Kakasi->new;
$k->set('-w'); # Text::Kakasi::getopt_argv('-w');

Unlike getopt_argv(), which returns the status, "set" returns the object itself, so you can go full main-character and chain it like this:

my $tokenized = $k->set('-w')->get($raw_japanese);

To get the status of "$k->set", use "$k->error".

See also "Perl 5.8 Features" 🧚


⭐ $k->error

Returns the status of the last method. That's it, that's the whole vibe check πŸ“‹


πŸŽ€ $result = $k->get($raw_japanese);

The OOP interface to "do_kakasi". These two blocks of code are literally equivalent:

# Functional
getopt_argv('-w'); $result = do_kakasi($raw_japanese);
# OOP
$k->set('-w')->get($raw_japanese);

🌟 Perl 5.8 Features 🌟

Perl 5.8 introduced the Encode module, which transcodes various encodings. This module takes advantage of that feature β€” but to keep backward compatibility with version 1.x, the feature is enabled only when you use the OOP interface (version 1.x only provided functional APIs, so there's the reason, iykyk).

On Perl 5.8 and up, "-iencoding" and "-oencoding" are handled by the Encode module, so you can use encodings Kakasi does not support β€” like utf8 πŸ¦„ In other words:

$result = $k->set(qw/-iutf8 -outf8 -w/)->get($utf8);

Is analogous to:

$euc = encode('eucjp' => $utf8);
getopt_argv('-w');
$tmp = do_kakasi($euc);
$result = decode('eucjp' => $tmp);

When you specify "-outf8", "$k->get" will return the string with the utf8 flag on 🚩

And if you wanna suppress this whole feature? Just set $Text::Kakasi::HAS_ENCODE to 0, in which case the feature is not used πŸ™…β™€οΈ


πŸ”₯ SEE ALSO πŸ”₯


(C) 1998, 1999, 2000 NOKUBI Takatsugu <knok@daionet.gr.jp>
(C) 2003 Dan Kogai <dankogai@dan.co.jp>
There is no warranty for this free software, bestie πŸ’€ Anyone can modify and/or redistribute this module under the GNU GENERAL PUBLIC LICENSE. See the COPYING file that's included in the archive for more details πŸ“œ

okay that's the whole page, and before you go β€” this one's stamped perl v5.42.3, dated 2026-08-19 🫢 go forth and invert some kanji, you absolute icon πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from