File::Find will handle recursive directory spanning.
Here's a simple sub which takes a filepath and a string, returning true if the file contains the string. Slurp is used, so files should be smallish.
That should be fairly quick.sub contains { my ($file, $string) = @_; my $content; { local $/; open my $fh, '<', $file or die $!; $content = <$fh>; close $fh or die $!; } index( $content, $string) == -1 ? 0 : 1; }
Update: Any sub returns the value of its last expression unless an explicit return comes before. In sub contains, the trinary operator ?: tests whether index returned -1, and evaluates to zero if so, otherwise evaluates to 1. That could as well have been written return not index( $content, $string) == -1 or return index( $content, $string) != -1 where the return keyword is optional.
After Compline,
Zaxo
In reply to Re: Easy way to search files?
by Zaxo
in thread Easy way to search files?
by matth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |