#!/usr/bin/perl -w use strict; my $count; my $word = "this"; while () { my @matches = /\b\Q$word\E\b/gi; $count += @matches; # # or just do this: # $count+= () = /\b\Q$word\E\b/gi; } print "$count"; #prints 3 # note that the last "thisthis" on line 1 # doesn't count! Also note that multiple "this" on # same line are counted. __DATA__ this is the text that this thisthis abd this