#!/usr/bin/perl use strict; use warnings; my $Usage = "Usage: $0 [-p path/to/search] regex\n"; if ( @ARGV > 2 and $ARGV[0] eq '-p' ) { shift; chdir $ARGV[0] or die "Can't chdir to $ARGV[0]: $!\n"; shift; } die $Usage unless ( @ARGV == 1 ); my $regex = shift; opendir( D, '.' ); my @files = grep { -f } readdir D; # we only want to look at data files my @matches; for my $f ( @files ) { open( F, $f ) or do { warn "open failed for $f: $!\n"; next; }; while () { if ( m{$regex} ) { push @matches, $f; last; } } } print "The pattern {$regex} was found in ", scalar @matches, " files:\n"; print "@matches\n";