Which returns a subroutine that can do the desired translation on the fly. Note that it works on $_ if a parameter isnt supplied, and returns the same thing that tr/// would. Itll die if the eval fails.use strict; use warnings; sub make_tr { my $from=quotemeta(shift); my $to =quotemeta(shift); (my $opts=shift || "")=~tr/cdsCDS/cdscds/d; my $tr=eval "sub { (defined \$_[0] ? \$_[0] : \$_)=~tr/$from/$ +to/$opts}"; die "$@" if $@; return $tr; } my $tr=make_tr('A-Z','a-z'); my $x="YaBaDaBaDo"; $tr->($x); print $x,"\n"; $_="FOO"; $tr->(); print; __END__ yabadabado foo
Good Luck.
update Forgot the quotemeta() on the first post... And beefed up the $opts cleaning... And added the my()s that are in my local version but not in the original. Sigh.
--- demerphq
my friends call me, usually because I'm late....
In reply to Re: Transliteration!!!
by demerphq
in thread How to eval a tr/// (was: Transliteration!!!)
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |