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;