in reply to Counting text strings
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.use strict; use warnings; my $file = do { local $/; <> }; my $count = 0; ++$count while $file =~ /foo/g; print $count;
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
|
|---|