gpt4 book ai didi

Perl 文件测试运算符(operator)帮助

转载 作者:行者123 更新时间:2023-12-04 22:52:26 25 4
gpt4 key购买 nike

这是一个非常基本的问题,但我是 perl 的新手,无法弄清楚问题是什么。我只是想隔离目录中的文件,但 -d 运算符一直将所有文件夹内容视为文件...

@contents 是我的数组,当我运行它时:

use strict;

if ($ARGV[1]) {
die("Error: You can only monitor one directory at a time\n");
}

my $directory = $ARGV[0] || die "Error: No directory defined\n";

opendir(DIR, $directory) || die "Error: Can't open dir $directory: $!";
my @contents = readdir(DIR);

foreach my $item(@contents) {
if (-d $item) { next; }
print"$item is a file\n";
}

closedir (DIR);

我不断收到文件夹和文件。或者,如果我使用 -f,我什么也得不到。

编辑:这是输出 -
file01.txt is a file
folder 01 is a file
folder 02 is a file
Screen shot 2010-04-18 at 1.26.17 PM.png is a file

我在 OSX 上运行这个

编辑:目录 ls -l输出:
aaron ~/Documents/test: ls -l
total 112
-rw-r--r--@ 1 aaron staff 51235 18 Apr 13:26 Screen shot 2010-04-18 at 1.26.17 PM.png
-rw-r--r--@ 1 aaron staff 7 18 Apr 13:26 file01.txt
drwxr-xr-x 3 aaron staff 102 18 Apr 13:25 folder 01
drwxr-xr-x 2 aaron staff 68 18 Apr 13:25 folder 02

最佳答案

解决方案

我正在测试 '.'作为目录...您正在使用其他目录进行测试。然后相对于当前目录检查从目录中读取的名称。如果我使用其他目录名,我将获得除“.”之外的几乎所有内容。和 '..' 列为文件,无论如何。

如果您使用 $ARGV[0] 的值作为名称前缀,您将得到预期的结果:

#!/bin/perl -w

use strict;

if ($ARGV[1]) {
die("Error: You can only monitor one directory at a time\n");
}

my $directory = $ARGV[0] || die "Error: No directory defined\n";

opendir(DIR, $directory) || die "Error: Can't open dir $directory: $!";
my @contents = readdir(DIR);

foreach my $item(@contents) {
next if -d "$ARGV[0]/$item";
print "$ARGV[0]/$item is a file\n";
}

closedir (DIR);

之前的解释尝试

这适用于 MacOS X:
#!/bin/perl -w
use strict;

my @contents = <*>;

foreach my $item (@contents)
{
print "== $item\n";
next if -d $item;
print "$item is a file\n";
}

测试:
MiniMac JL: perl -c xx.pl
xx.pl syntax OK
MiniMac JL: perl xx.pl
== cproto-4.7g
== fpqsort1
fpqsort1 is a file
== fpqsort1.h
fpqsort1.h is a file
== fpqsort2
fpqsort2 is a file
== fpqsort2.c
fpqsort2.c is a file
== gcc-predef.h
gcc-predef.h is a file
== git-1.6.5.7
== go
== makefile
makefile is a file
== qs-test1.c
qs-test1.c is a file
== qs-test2.c
qs-test2.c is a file
== RCS
== rep-report.txt
rep-report.txt is a file
== select.c
select.c is a file
== soq
== xx.pl
xx.pl is a file
MiniMac JL:

鉴于问题中代码的略微修改版本:
#!/bin/perl -w

use strict;

if ($ARGV[1]) {
die("Error: You can only monitor one directory at a time\n");
}

my $directory = $ARGV[0] || die "Error: No directory defined\n";

opendir(DIR, $directory) || die "Error: Can't open dir $directory: $!";
my @contents = readdir(DIR);

foreach my $item(@contents) {
print "<<$item>>\n";
next if -d $item;
print"$item is a file\n";
}

closedir (DIR);

在与以前相同的目录上运行它,我得到输出:
Minimac JL: perl yy.pl .
<<.>>
<<..>>
<<cproto-4.7g>>
<<fpqsort1>>
fpqsort1 is a file
<<fpqsort1.h>>
fpqsort1.h is a file
<<fpqsort2>>
fpqsort2 is a file
<<fpqsort2.c>>
fpqsort2.c is a file
<<gcc-predef.h>>
gcc-predef.h is a file
<<git-1.6.5.7>>
<<go>>
<<makefile>>
makefile is a file
<<qs-test1.c>>
qs-test1.c is a file
<<qs-test2.c>>
qs-test2.c is a file
<<RCS>>
<<rep-report.txt>>
rep-report.txt is a file
<<select.c>>
select.c is a file
<<soq>>
<<xx.pl>>
xx.pl is a file
<<yy.pl>>
yy.pl is a file
Minimac JL:

请注意 Perlish 习语 ' next if -d $item; '。还要注意调试技巧:在名称通过数组时打印名称 - 使用“<<”和“>>”包围名称有助于识别奇怪的副作用(例如名称中的换行符)。我确实仔细检查了提供的代码是否产生相同的结果 - 确实如此。我在 MacOS X 10.6.3 和 Perl 上运行。

关于Perl 文件测试运算符(operator)帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2661087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com