opendir(DIR, "dir") or die "$!";
my @txt_files = grep {/\.txt$/} readdir(DIR);
if (scalar @txt_files > 0) {
#do stuff
}
Cheers.
BazB
Update: Ooops. Fixed misplaced bracket.
| [reply] [d/l] |
Wildcards won't work with the filetest operator. Try readdir and grep, ie:
opendir DIR, '.' or die "Error: $!";
my @files = grep { /\.txt$/ } readdir DIR;
will find *.txt in the current directory.
| [reply] [d/l] |
print "Found textfile!" if <$dir/*.txt>
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] |
Beware that this executes the glob in a scalar context, which will really confuse
you when you reexecute the code, since a scalar glob has local magical state.
For example, run:
foreach $n (1..50) {
print "$n: ";
print "yes" if -e <*>;
print "\n";
}
If you have seven files, every 7th number will be missing, because the glob
has run out, and has returned undef, and is resetting.
This is better, as it uses glob in a list context:
print "Found textfile!" if () = <$dir/*.txt>;
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] [select] |
or if you're wed to the glob:
map { $txt = 1 if -e } <*.txt>;
-derby | [reply] [d/l] |