in reply to count number of occurrences of specific character(s) in a string

I don't get it, why do you include | in the delimiter? Do you also want to count that? it has no special meaning in tr.

I'd write that as

sub count_separators { my $value = shift; my $delim = shift; my %map = ( '(' => '()', '{' => '{}', '"' => '"'; ); my $tr = $map{$delim} or die "Not a valid separator"; return eval "\$value =~ tr/$tr/;" }

(untested).