View on
Ben Bullock > Convert-Moji-0.08 > Convert::Moji



Annotate this POD

View/Report Bugs
Module Version: 0.08   Source   Latest Release: Convert-Moji-0.09


Convert::Moji - objects to convert alphabets


    # Examples of rot13 transformers:
    use Convert::Moji;
    # Using a table
    my %rot13;
    @rot13{('a'..'z')} = ('n'..'z','a'..'m');
    my $rot13 = Convert::Moji->new (["table", \%rot13]);
    # Using tr
    my $rot13_1 = Convert::Moji->new (["tr", "a-z", "n-za-m"]);
    # Using a callback
    sub rot_13_sub { tr/a-z/n-za-m/; return $_ }
    my $rot13_2 = Convert::Moji->new (["code", \&rot_13_sub]);
    # Then to do the actual conversion
    my $out = $rot13->convert ("secret");
    # You also can go backwards with
    my $inverted = $rot13->invert ("frperg");
    print "$out\n$inverted\n";




This documents version 0.08 of Convert::Moji corresponding to git commit 7791e4bab1af7a1a2d14e505aa7039dceb98b66f released on Wed Feb 3 20:45:28 2016 +0900.


Convert::Moji creates objects which can be used to convert between different alphabets.

This is basically a helper module for Lingua::JA::Moji. It was split out of that module as a general-purpose converter for any alphabets.



    my $convert = Convert::Moji->new (["table", $mytable]);

Create the object. Arguments are a list of array references. The array references should have either the "noninvertible" flag "oneway" or one of the following as its first argument.

You can also chain the converters together, with

    my $does_something = Convert::Moji->new (["table", $mytable],
                                             ["tr", $left, $right]);

After this comes one more argument, a reference to the hash containing the table. For example

     my $conv = Convert::Moji->new (["table", \%crazyhash]);

The hash keys and values can be any length, so you can convert single characters into words, as in

     my %crazyhash = {"a" => "apple", "b" => "banana"}

and vice-versa if you wish. The conversion will be performed correctly regardless of the weirdness of your table.


After this comes one more argument, the name of a file containing some information to convert into a hash table. The file format is space-separated pairs, no comments or blank lines allowed. If the file does not exist or cannot be opened, the module prints an error message, and returns the undefined value.


After this comes one or two references to subroutines. The first subroutine is the conversion and the second one is the inversion routine. If you omit the second routine, it is equivalent to specifying "oneway".


After this come two arguments, the left and right hand sides of a "tr" expression, for example

     Convert::Moji->new (["tr", "A-Z", "a-z"])

will convert upper to lower case

A "tr" is performed, and inversely for the invert case.

Conversions, via "convert", will be performed in the order of the arguments. Inversions will be performed in reverse order of the arguments, skipping uninvertibles.

Uninvertible operations

If your conversion doesn't actually go backwards, you can tell the module when you create the object using a keyword "oneway":

    my $uninvertible = Convert::Moji->new (["oneway", "table", $mytable]);

Then $uninvertible->invert doesn't do anything. You can also selectively choose which operations of a list are invertible and which aren't, so that only the invertible ones do something.

Load from a file

Load a character conversion table from a file using

Convert::Moji->new (["file", $filename]);

In this case, the file needs to contain a space-separated list to be converted one into the other.


This doesn't handle comments or blank lines in the file.


The convert method takes one argument, which is a scalar string to be converted into the other list by the stuff we fed in at "new".


no "strict conversion"

Just ignores (passes through) characters which it can't convert. It should have a "strict" option to also validate the input.


Inverts the input.

Takes two arguments, the first is the string to be inverted back through the conversion process, and the second is the type of conversion to perform if the inversion is ambiguous. This can take one of the following values


If the inversion is ambiguous, it picks the first one it finds.


If the inversion is ambiguous, it picks one at random.


In this case you get an array reference back containing either strings where the inversion was unambiguous, or array references to arrays containing all possible strings. So it's a horrible mess.


Like "all" but you get a scalar with all the options in square brackets instead of lots of array references.


second argument not implemented fully

The second argument part is only implemented for hash table based conversions, and is very likely to be buggy even then.


These functions are used by the module and may be useful to outside programs.


    # Returns false:
    length_one ('x', 'y', 'monkey');
    # Returns true:    
    length_one ('x', 'y', 'm');

Returns true if every element of the array has a length equal to one, and false if any of them does not have length one.


    my $regex = make_regex (qw/a b c de fgh/);

    # $regex = "fgh|de|a|b|c";

Given a list of inputs, make a regular expression which matches any of the characters in the list of inputs, longest match first. Each of the elements of the list is quoted using quotemeta. The regular expression does not contain capturing parentheses. For example, to convert everything in string $x from the keys of %foo2bar to its values,

    my %foo2bar = (mad => 'max', dangerous => 'trombone');
    my $x = 'mad, bad, and dangerous to know';
    my $regex = make_regex (keys %foo2bar);
    $x =~ s/($regex)/$foo2bar{$1}/g;
    # Now $x = "max, bad, and trombone to know".


    if (unambiguous (\%table)) {


Returns true if all of the values in %table are distinct, and false if any two of the values in %table are the same.




Ben Bullock, <>


If you'd like to see this module continued, let me know that you're using it. For example, send an email, write a bug report, star the project's github repository, add a patch, add a ++ on, or write a rating at CPAN ratings. It really does make a difference. Thanks.


This package and associated files are copyright (C) 2008-2016 Ben Bullock.

You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.

syntax highlighting: