NAME
Text::Transliterator - Wrapper around Perl tr/../../ operator
SYNOPSIS
my $tr = Text::Transliterator->new($from, $to, $modifiers);
# or
my $tr = Text::Transliterator->new(\%map, $modifiers);
$tr->(@strings);
DESCRIPTION
This package is a simple wrapper around Perl's transliteration operator tr/../../..
. Starting either from two strings of characters, or from a map of characters, it will compile a function that applies the transliteration to any list of strings.
This does very little work, and therefore would barely merit a module on its own; it was written mainly for serving as a base package for Text::Transliterator::Unaccent. However, in some situations it may be useful in its own right, since Perl has no construct similar to qr/.../
for "compiling" a transliteration. As a matter of fact, the tr/../../
documentation says "if you want to use variables, you must use an eval()" ... which is what the present module does, albeit in a somewhat more controlled way.
METHODS
new
my $tr = Text::Transliterator->new($from, $to, $modifiers);
# or
my $tr = Text::Transliterator->new(\%map, $modifiers);
Creates a new transliterator function.
In the first syntax, the arguments are two strings that will be passed directly to the tr/.../.../
operator (see "Regexp Quote-Like Operators" in perlop), i.e. a string of characters to be replaced, and a string of replacement characters. The third argument $modifiers
is optional and may contain a string with any combination of the cdsr
modifiers to the tr/.../.../
operator.
In the second syntax, the argument is a hashref, in which keys are the characters to be replaced, and values are the replacement characters. Optional $modifiers
are as above.
Unlike usual object-oriented modules, here the return value from the new
method is a reference to a function, not an object. That function should be called as
$tr->(@strings);
and modifies every member of @strings
. By default strings are modified in place,like with the tr/.../.../
operator, unless the r
modifier is present.
The function returns the list of results of the tr/.../.../
operation on each of the input strings. By default this will be the number of transliterated characters for each string. If the r
modifier is present, the return value is the list of transliterated strings. In scalar context, the last member of the list is returned (for compatibility with the previous API).
AUTHOR
Laurent Dami, <dami@cpan.org>
LICENSE AND COPYRIGHT
Copyright 2010-2025 Laurent Dami.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.