Because the transliteration table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote interpolation. That means that if you want to use variables, you must use an eval():
eval "tr/$oldlist/$newlist/"; die $@ if $@;
eval "tr/$oldlist/$newlist/, 1"; die $@ if $@;
In your case that would be
my $count = eval "\$str =~ tr/$char//" or die $@ if $@;
In reply to Re: Confused by RegEx count
by BillKSmith
in thread Confused by RegEx count
by Melly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |