in reply to Variables in Regular Expressions?
tr/// does not use regular expressions so your title has thrown everyone off.
tr/// only handles compile-time values so you have to use eval to get tr/// to deal with variable data.
If you are going to use the tr/// more than once, then for efficiency, you should do something like:
- tye (but my friends call me "Tye")sub generateTr { my( $from, $to )= @_; my $sub= eval <<END; sub { my( \$copy )= \@_; \$copy =~ tr/\Q$from\E/\Q$to\E/; return \$copy; } END die "$@" if $@; return $sub; } my $xlate= generateTr( "abc", "def" ); my $c= $xlate->( "abc leppard" ); # $c is now "def leppdrd"
|
|---|