use 5.010; use Path::Class qw(dir file); say "GOT $_" for grep_dir_recursively( qr(say), # pattern to grep for (with smartmatch) qr(\.pl$), # pattern to match file names (with smartmatch) '/home/tai/tmp', # directory to search ); sub grep_dir_recursively { my $text_pattern = shift; my $file_pattern = shift; my @results; for my $root (@_) { dir($root)->recurse(callback => sub { my $file = shift; return if $file->is_dir; return unless $file ~~ $file_pattern; my $fh = $file->open; while (<$fh>) { if ($_ ~~ $text_pattern) { push @results, $file; last; } } }); } return @results; }