The documentation for
tr clearly states that it does not interpolate. As a workaround, we can interpolate into a
string and then evaluate that string. The first two tests below demonstrate that we build the expected string and that it evaluates correctly. As a notational convenience, we can combine these two steps into one statement. The third test below demonstrates that form.
use strict;
use warnings;
use Test::Simple tests=>3;
my $kards;
my $bad = 'AKQJT98765432KKKK';
my $card = 'K';
my $string = "\$bad =~ tr/$card//";
ok($string eq '$bad =~ tr/K//', "string = '$string'");
$kards = eval $string;
ok($kards == 5, "Number of K's in $bad is $kards");
$kards = eval "\$bad =~ tr/$card//";
ok($kards == 5, "Combined form");
OUTPUT:
1..3
ok 1 - string = '$bad =~ tr/K//'
ok 2 - Number of K's in AKQJT98765432KKKK is 5
ok 3 - Combined form