2012-08-25 59 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') { ... } 
相關問題