Hint: Use the function 'glob' (refer perldoc -f glob) to select the text files. For each file, open the file, and read it line-by-line. Increment the count for each line that matches your string. Close the file. When you are done with all the files, print the count. Good Luck, Bill

It's not necessary to use the module File::Find, in this case, as directory recursion's not needed. This is why BillKSmith suggested using glob for obtaining the list of text files in the specified directory.

Given the above, consider the following:

Code

use strict; use warnings;

my $dir = '.'; my $count = 0;

for (<$dir/*.txt>) { push @ARGV, $_;

while (<>) { $count++ if /\bTEST\b/; } }

print $count;

The <$dir/*.txt> notation globs all text files in the directory $dir, returning a list of file text file names. The for loop implicitly assigns each one of these file names to Perl's default scalar, $_. The file name in $_ is pushed onto @ARGV, so we can let Perl take care of opening and closing the file for reading. The while (<>) { notation iterates through each file's lines (<> means <ARGV> and we just placed the file's name in @ARGV). The regex /\bTEST\b/ (implicitly matched against $_, in this case) is used to match the word on each line. Note that the search word is enclosed by \b; this means word boundary, since it seems that you don't want an in-string match. The variable $count is incremented for each successful match. Finally, the variable $count is printed.

If the above is just all too unfamiliar at this point, here's a functionally equivalent alternative: