in reply to Tricky regexp

First, if I understand your stated task, the common unix/gnu "grep" command already does what you want:
cd /path/to/search grep -l pattern_to_find *
The two shell commands above do exactly what you were trying to do in perl (and if you're on a windows system, the gnu "bash" shell and "grep" are available for your OS -- since you have perl, you should know about these other tools).

There are a few problems with the OP script:

I'm not sure if you've given us the exact wording of the error message you got, and I'm not sure why you got a message like "readline() on closed filehandle" -- but that's the least of your problems. If you really don't want to use the existing "grep" command (e.g. if you want to use a regex that only Perl will support), then try something like this:
#!/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 fil +es my @matches; for my $f ( @files ) { open( F, $f ) or do { warn "open failed for $f: $!\n"; next; }; while (<F>) { if ( m{$regex} ) { push @matches, $f; last; } } } print "The pattern {$regex} was found in ", scalar @matches, " files:\ +n"; print "@matches\n";