Tr(3)
π NAME π
okay so this is Tr(3), straight out of the User Contributed Perl Documentation, and it's literally all about Regexp::Tr β run-time-compiled tr/// objects. periodt. π
β¨ SYNOPSIS β¨
bestie, here's the vibe in code:
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!
π¦ ABSTRACT π¦
It solves the whole "you need compile-time knowledge of tr/// constructs" problem by generating the tr/// at run-time and storing it as an object. iconic behavior, honestly. π
πΈ DESCRIPTION πΈ
One very useful ability of Perl is doing relatively cheap transliteration via the tr/// regex operator. slay. 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 use often β for instance, within a loop. This module solves that problem by compiling the tr/// a single time and letting you use it repeatedly and delete it when it's no longer useful. main character module energy fr fr. π
π§ User Efficiency Notes π§
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 separately for every package which uses this module, so multiple packages relying on this ability can still gain the speed benefit. iykyk. π
This cache may be emptied β and 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. like, don't do extra work for no reason, bestie. π«Ά
π METHODS π
CLASS->new(FROMSTRING,TOSTRING,[MODIFIERS])
This creates a new instance of this object. FROMSTRING is the precursor string for the tr/// β eg: "a-z". TOSTRING is the successor string for the tr/// β eg: "bac-z". The optional MODIFIERS is a string containing any modifiers to the tr/// β eg: "e", etc. so many possibilities, so little time. β
$obj->bind(SCALARREF)
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. yes, you gotta do the backslash yourself β no cap. π
$obj->trans(SCALAR)
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. context matters, bestie. π¦
CLASS->flush()
Flushes the efficiency cache, potentially gaining some memory back but forcing the next object to be created entirely from scratch. Returns void. it's giving spring cleaning but for memory. π§Ήβ¨
π₯ SEE ALSO π₯
perlopβ provides a definition of thetr///operator.perlreβ provides more information on the operator.
π AUTHOR π
Robert Fischer, <chia@cpan.org> β Hamline University, class of 2004. shoutout to the author, fr fr. π
π COPYRIGHT AND LICENSE π
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.
mwah π β this page came from perl v5.42.2, dated 2023-11-29, and it's the manual page for Tr(3). you're welcome, bestie π
β¨
π¦ where this page comes from
- alpine 3.24: perl-regexp-tr-doc 0.05-r4 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms