in reply to How to eval a tr/// (was: Transliteration!!!)
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....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Transliteration!!!
by Aristotle (Chancellor) on Oct 20, 2002 at 20:36 UTC | |
by demerphq (Chancellor) on Oct 20, 2002 at 20:38 UTC | |
by Anonymous Monk on Oct 20, 2002 at 21:47 UTC | |
by demerphq (Chancellor) on Oct 20, 2002 at 22:47 UTC |