在系统中的特定文件夹中有一个文件。我知道它的名字是一个数字,我在一个变量中有这个数字。
我不知道这个文件有什么扩展名。
我如何舀起这个文件?
(my $filename) = glob "$specific_folder_in_the_system/$that_number.*";
编辑:正如 Ikegami 在下面指出的,这对目录的特定路径名很敏感。如果该目录名称包含空格或其他特殊字符,它将失败。您可以通过将字符串的非通配符部分包含在嵌入式引号中来缓解这种情况:
(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
先使用 just glob("$that_number.*")
,然后再使用,只要$that_number
它确实是一个数字就应该是安全的。
您还可以使用opendir
和的组合grep
来代替glob
:
opendir(my $dir, $specific_folder_in_the_system) or die;
(my $filename) = grep /^$that_number\./ readdir $dir;
my ( $file ) = glob "$num.*" ;