my $randomNum = getMyRandomNum(); my $directory = "/path/to/some/dir"; openddir DIR, $directory or die "opening $directory failed"; my $specialRegex = qr/$randomNum.*log$/i; my @logfiles = grep /$specialRegex/, readdir DIR; for my $logfile (@logfiles) { open LOG "<", $logfile or warn "...."; while () { print if dateMatches() .. 0; # should print all lines from where dateMatches() returns TRUE. ... } }