This may prove a bit more efficient:
$count = () = /$keyword/ig;
It puts the match into array context, which causes it to return the matches; then the resulting array is taken as a scalar, resulting in the count. This is a somewhat common Perl idiom. And you don't have to reset the counter.
Of course, if you need to accumulate several counts, make it += instead of =, and do remember to reset the counter.
Caution: Contents may have been coded under pressure.
|