in reply to Counting text strings

... if you are really only interested in the number of occurrences and the files your are dealing with aren't extremely large, you could also read in the whole file at once. The follwing assumes that the filename was given as an argument on the command line.
use strict; use warnings; my $file = do { local $/; <> }; my $count = 0; ++$count while $file =~ /foo/g; print $count;
A few explanations: 'do' sets up a block and evaluates to the last statement in this block. Then we set the global variable $/ locally to undef (that means its former value is automatically restored when the block is exited). Setting this value to undef results in Perl going into slurp mode, i.e. it reads till the end of a file. The magic <> opens the command line arguments one at a time and (in scalar context) returns one line - and in slurp mode the whole file. Voila, $file contains now the whole contents.

Enlil already gave the good explanation of the next lines and this works in the same way for one line and for a whole file. In this context it might be interesting for you to look up the /m and /s modifiers for regexes.

-- Hofmator