First shorter does not mean clearer (nor better), but it is possible to shorten your code to something like so:
#!d:\perl\bin\perl.exe
$pth = "\\\\127.0.0.1\\bex";
opendir(Directory, $pth) or die "Cannot Open Directory";
@contents = grep {
m!^[Bb].+\.txt$! #assuming .txt ending
and
-M "$path\\$_" > 1
} readdir(Directory);
closedir(Directory);
A couple of other issues though. You do not need the splice as your regular expression would have eliminated it anyhow. and second I changed your regular expression because if you want to match a period in a regular expression you can do it one of two ways backwack it (i.e.
\. )or in a character class (i.e.
[.] ), the
.txt in your regular expression would also match anything that ended with txt preceded by any character.
update:A shorter solution for the whole thing (untested):
#!d:\perl\bin\perl.exe
my $pth = "\\\\127.0.0.1\\bex";
opendir(Directory, $pth) or die "Cannot Open Directory";
while ( $_ = readdir(Directory) ) {
if ( m!^[Bb].+\.txt$! and -M "$path\\$_" > 1 ) {
open ( FILE, "$path\\$_" > 1 ) or die "$!";
while ( my $line = <FILE> ) {
print $line;
}
close(FILE);
}
}
closedir(Directory);
-enlil