I really like Path::Class::Rule for searching the filesystem. Its author (David Golden) has recently been working on a faster replacement though (Path::Iterator::Rule) and your question has given me a chance to experiment with it...
use 5.010; use strict; use warnings; use Path::Iterator::Rule; use Path::Tiny; # The directories we're looking in. # my @search_dirs = ('/home/tai/tmp'); # The regular expression we're searching for. # my $search_for = qr{foo}; "Path::Iterator::Rule"->add_helper( contents_match => sub { my $regexp = shift; sub { my $item = path(shift); return unless $item->is_file; $item->slurp_utf8 =~ $regexp; }; }, ); "Path::Iterator::Rule"->add_helper( line_match => sub { my $regexp = shift; sub { my $item = path(shift); return unless $item->is_file; my $fh = $item->openr_utf8; while (my $line = <$fh>) { return 1 if $line =~ $regexp; } return; }; }, ); my @matches = "Path::Iterator::Rule" -> new -> file -> name(qr{\.txt$}) -> line_match($search_for) -> all(@search_dirs) ; say for @matches;
In reply to Re: pattern search in all the files in a directory
by tobyink
in thread pattern search in all the files in a directory
by Bharath666
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |