Regexp::Tr - Run-time-compiled tr/// objects.
use Regexp::Tr; my $trier = Regexp::Tr->new("a-z","z-a"); my $swapped = "foobar"; $trier->bind(\$swapped); # $swapped is now "ullyzi" my $tred = $trier->trans("barfoo"); # $tred is "yziull" Regexp::Tr->flush(); # Cache is gone!
Solves the problem requiring compile-time knowledge of tr/// constructs by generating the tr/// at run-time and storing it as an object.
One very useful ability of Perl is to do relatively cheap transliteration via the tr/// regex operator. Unfortunately, Perl requires tr/// to be known at compile-time. The common solution has been to put an eval around any dynamic tr/// operations, but that is very expensive to be used often (for instance, within a loop). This module solves that problem by compiling the tr/// a single time and allowing the user to use it repeatedly and delete it when it it no longer useful.
The last instance created is stored for efficient recreation. This is useful for repeated iterations over the same code (for instance, within a loop). The last instance created is stored seperately for every package which uses this module, so multiple packages relying on this ability can still gain the speed benefit.
This cache may be emptied may be regained at any time by calling the class method CLASS->flush(). All objects will continue to function, but the internal cache will be emptied. This is probably not worth it unless there are many different namespaces using this package or the program is very memory-sensitive.
This creates a new instance of this object. FROMSTRING is the precursor string for the tr/// (eg: "a-z"), TOSTRING is the succsessor string for the tr/// (eg: "bac-z"), and the optional MODIFIERS is a string containing any modifiers to the tr/// (eg: "e", etc.).
This binds the given SCALARREF and then performs the object's tr/// operation, returning what the tr/// operation will return. Note that this method does not create the reference, so the user is responsible for backslashing the variable.
This takes a scalar, performs the tr/// operation, and returns the tr///ed string in scalar context, or a list consisting of the tr///ed string and the tr/// return value in list context.
Flushes the efficiency cache, potentially gaining some memory back but forcing the next object to be created entirely from scratch. Returns void.
Provides a definition of the tr/// operator.
Provides more information on the operator.
Robert Fischer, <firstname.lastname@example.org> Hamline University, class of 2004.
Copyright 2003 by Robert Fischer
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.