2012-07-03 90 views

回答

3
(my $filename) = glob "$specific_folder_in_the_system/$that_number.*"; 

編輯:作爲池上低於所指出的,這是對目錄的具體路徑敏感。如果該目錄名稱包含空格或其他特殊字符,則會失敗。您可以減輕在嵌入式引號包裹字符串的非通配符部分:

(my $filename) = glob "'$specific_folder_in_the_system/$that_number.'*"; 

但是,這仍然會失敗例如$specific_folder_in_the_system = "/Users/O'Neal, Patrick";

如果您不介意更改您當前的工作目錄,您可以先chdir($specific_folder_in_the_system) or die,然後再使用glob("$that_number.*"),只要$that_number確實是一個數字,這應該是安全的。

您還可以使用的opendirgrep組合代替glob

opendir(my $dir, $specific_folder_in_the_system) or die; 
(my $filename) = grep /^$that_number\./ readdir $dir; 
+0

音符保持在左側爲否則怪異意想不到的事情可能會開始發生該支架的重要性。 –

+0

失敗'$ specific_folder_in_the_system ='/ foo bar';' – ikegami

+0

@ikegami:好點,請參閱編輯。 –

2
my ($file) = glob "$num.*" ;