list 如何在 Perl 中获取目录列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/205159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:18:24  来源:igfitidea点击:

How do I get a directory listing in Perl?

perllistdirectory

提问by user28280

I would like to execute lsin a Perl program as part of a CGI script. For this I used exec(ls), but this does not return from the execcall.

我想ls在 Perl 程序中作为 CGI 脚本的一部分执行。为此,我使用了exec(ls),但这不会从exec调用中返回。

Is there a better way to get a listing of a directory in Perl?

有没有更好的方法来获取 Perl 中的目录列表?

回答by Leon Timmermans

Exec doesn't return at all. If you wanted that, use system.

Exec 根本不返回。如果需要,请使用系统。

If you just want to read a directory, open/read/close-dir may be more appropriate.

如果你只是想读取一个目录,open/read/close-dir 可能更合适。

opendir my($dh), $dirname or die "Couldn't open dir '$dirname': $!";
my @files = readdir $dh;
closedir $dh;
#print files...

回答by brian d foy

Everyone else seems stuck on the exec portion of the question.

其他人似乎都停留在问​​题的执行部分。

If you want a directory listing, use Perl's built-in globor opendir. You don't need a separate process.

如果您想要目录列表,请使用 Perl 的内置globopendir. 您不需要单独的过程。

回答by J.J.

execdoes not give control back to the perl program. systemwill, but it does not return the results of an ls, it returns a status code. tick marks ``will give you the output of our command, but is considered by some as unsafe.

exec不会将控制权交还给 perl 程序。 system会,但它不返回 ls 的结果,它返回一个状态代码。刻度线``会给你我们命令的输出,但被一些人认为是不安全的。

Use the built in dir functions. opendir, readdir, and so on.

使用内置的 dir 函数。opendir、readdir 等。

http://perldoc.perl.org/functions/opendir.html

http://perldoc.perl.org/functions/opendir.html

http://perldoc.perl.org/functions/readdir.html

http://perldoc.perl.org/functions/readdir.html

回答by holli

In order to get the output of a system command you need to use backticks.

为了获得系统命令的输出,您需要使用反引号。

$listing = `ls`;

However, Perl is good in dealing with directories for itself. I'd recommend using File::Find::Rule.

然而,Perl 擅长为自己处理目录。我建议使用 File::Find::Rule。

回答by Greg

Use Perl Globbing:

使用 Perl 通配符:

my $dir = </dir/path/*> 

回答by dynax60

Yet another example:

再举一个例子:

chdir $dir or die "Cannot chroot to $dir: $!\n";
my @files = glob("*.txt");

回答by Octoberdan

EDIT: Whoops! I thought you just wanted a listing of the directories... remove the 'directory' call to make this script do what you want it to...

编辑:哎呀!我以为您只想要目录列表...删除“目录”调用以使此脚本执行您想要的操作...

Playing with filehandles is the wrong way to go in my opinion. The following is an example of using File::Find::Rule to find all the directories in a specified directory. It may seem like over kill for what you're doing, but later down the line it may be worth it.

在我看来,使用文件句柄是错误的方法。下面是使用 File::Find::Rule 查找指定目录中所有目录的示例。对于你正在做的事情来说,这似乎有点过头了,但后来它可能是值得的。

First, my one line solution:

首先,我的单行解决方案:

File::Find::Rule->maxdepth(1)->directory->in($base_dir);

Now a more drawn out version with comments. If you have File::Find::Rule installed you should be able to run this no problem. Don't fear the CPAN.

现在是一个带有评论的更详尽的版本。如果您安装了 File::Find::Rule,您应该能够运行它没有问题。不要害怕 CPAN。

#!/usr/bin/perl

use strict;
use warnings;

# See http://search.cpan.org/~rclamp/File-Find-Rule-0.32/README
use File::Find::Rule;

# If a base directory was not past to the script, assume current working director
my $base_dir = shift // '.';
my $find_rule = File::Find::Rule->new;

# Do not descend past the first level
$find_rule->maxdepth(1);

# Only return directories
$find_rule->directory;

# Apply the rule and retrieve the subdirectories
my @sub_dirs = $find_rule->in($base_dir);

# Print out the name of each directory on its own line
print join("\n", @sub_dirs);

回答by JDrago

On Linux, I prefer find:

在 Linux 上,我更喜欢查找:

my @files = map { chomp; $_ } `find`;

回答by dsm

I would recommend you have a look at IPC::Open3. It allows for far more control over the spawned process than system or the backticks do.

我建议你看看IPC::Open3。与 system 或反引号相比,它允许对生成的进程进行更多的控制。