use strict; use warnings; my $file = do { local $/; <> }; my $count = 0; ++$count while $file =~ /foo/g; print $count;