2012-08-25 125 views

回答

4

只需用水珠:

if (<*.txt>) { ... } 
+5

...或者,因为人们总是习惯于看到用来从文件中读取,'水珠尖括号(“ * TXT”)'确实几乎完全一样的东西,而不太容易出错。 – DavidO

0
#! /usr/bin/perl 

my $dir = '.'; # current dir 

opendir(DIRHANDLE, $dir) || die "Couldn't open directory $dir: $!\n"; 
my @txt = grep { /\.txt$/ && -f "$dir/$_" } readdir DIRHANDLE; 
closedir DIRHANDLE; 

print ".txt files found" if (scalar(@txt)); 

作为奖励,发现所有.txt文件都在阵列@txt。

+0

不需要'标量'。 '打印“.txt文件发现”if @ txt“更好 – Borodin

0

请记住,*.txt实际上可能是一个目录。如果是文件,你是后,grepglob

if (grep -f, glob '*.txt') { ... } 
相关问题