启用目录浏览
It's very simple to print a list of all files in a directory using the built-in Perl glob function. Let's look over a short script that globs and prints a list of all files, in the directory containing the script itself.
这是非常简单的打印在所有文件的清单目录中使用内置的Perl水珠功能。 让我们看一个简短的脚本,该脚本在包含脚本本身的目录中遍历并打印所有文件的列表。
Perl Glob函数的示例 ( Examples of the Perl Glob Function )
#!/usr/bin/perl -w
@files = <*>;
foreach $file (@files) {
print $file . "\n";
}
When you run the program, you'll see it output the filenames of all files in the directory, one per line. The glob is happening on the first line, as the <*> characters pulls the filenames into the @files array.
运行该程序时,您会看到它输出目录中所有文件的文件名,每行一个。 当<*>字符将文件名拖到@files数组中时,就会在第一行发生全局错误。
@files = <*>;
Then you simply use a foreach loop to print out the files in the array.
然后,您只需使用foreach循环即可打印出阵列中的文件。
You can include any path in your filesystem between the <> marks. For example, say your website is in the /var/www/htdocs/ directory and you want a list of all the files:
您可以在文件系统中的<>标记之间包括任何路径。 例如,假设您的网站在/ var / www / htdocs /目录中,并且您想要所有文件的列表:
@files = </var/www/htdocs/*>;
Or if you just want a list of the files with the extension .html:
或者,如果您只想要扩展名为.html的文件列表:
@files = </var/www/htdocs/*.html>;
启用目录浏览