This works for me:
#!/usr/bin/perl
my $data = "this_is_a_test.txt";
$count = ($data =~ tr/_/_/);
print("Count is: $count\n");
[ir@grendel]# ./cchar.pl
Count is: 3
As an alternative, I wrote a small function also...
#!/usr/bin/perl
# Count letter frequency.
# Infinite Recursion - 13 JUL 2006
use strict;
my $base = "this_is_a_test.txt";
my $count = CountUnderscore($base);
print "Number of _ is: $count\n";
sub CountUnderscore ($base)
{
my @find = ('_');
my %freq = ();
foreach my $findc (@find)
{
my $count = 0;
foreach my $basec (split(//, $base))
{
if ($basec eq $findc)
{
++$count;
}
}
$freq{$findc} = $count;
}
return $freq{"_"};
}
[ir@grendel]# ./cfreq.pl
Number of _ is: 3