Perl文件测试操作员帮助

时间:2010-04-18 04:03:41

标签: perl

这是一个非常基本的问题,但我是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上运行它

编辑:dir 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

3 个答案:

答案 0 :(得分:5)

解决方案

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

如果您在名称前加上$ 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;'。还要注意调试技巧:在通过数组时打印名称 - 使用'&lt;&lt;'和'&gt;&gt;'包围名称有助于识别奇怪的副作用(例如名称中的换行符)。我仔细检查了提供的代码产生的结果是否相同 - 它确实如此。我正在运行带有Perl库存的MacOS X 10.6.3。

答案 1 :(得分:3)

来自readdir上的Perldoc:

  

如果您打算对其进行测试   从readdir返回值,你会   更好地预先添加目录   题。否则,因为我们没有   那里的chdir,本来就是   测试错误的文件。

调试代码:

#!/usr/bin/perl
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=$item\n";
    print "$item is a file \n" if (-f $directory."/".$item); 
    print"$item is a dir\n" if (-d $directory."/".$item);  
}

closedir (DIR);

答案 2 :(得分:2)

我猜你的@contents在文件末尾有换行符。尝试添加chomp

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