in reply to Easy way to search files?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Easy way to search files?
by matth (Monk) on Jan 06, 2003 at 14:42 UTC | |
by OM_Zen (Scribe) on Jan 06, 2003 at 18:42 UTC |