in reply to Re^2: doing tr/// on-the-fly?
in thread doing tr/// on-the-fly?

Okay, I didn't duplicate exactly what you said. The following code:
#!/usr/bin/env perl use strict; my $s = 'eabcde'; my $t = $s; print "$t\n"; $t =~ tr/abcd/efgh/; print "$t\n"; print '*' x 4, $/; my $search = 'abcd'; my $replace = 'efgh'; $t = $s; print "$t\n"; eval "\$t =~ tr/$search/$replace/"; print "$search\n"; print "$replace\n"; print "$t\n";
...does give me the expected results:
eabcde eefghe **** eabcde abcd efgh eefghe
Why the difference?

Replies are listed 'Best First'.
Re^4: doing tr/// on-the-fly?
by jwkrahn (Abbot) on Mar 18, 2010 at 01:43 UTC

    Because:

    eval { $t =~ tr/$search/$replace/; };

    is just normal perl code where there is no interpolation so '$' is replaced with '$' and 's' is replaced with 'r' and 'e' is replaced with 'e' and 'a' is replaced with 'p', etc. while using string eval interpolates the variables $search and $replace.