Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl脚本递归列出目录中的所有文件名_Perl_File_File Find - Fatal编程技术网

perl脚本递归列出目录中的所有文件名

perl脚本递归列出目录中的所有文件名,perl,file,file-find,Perl,File,File Find,我已经编写了以下perl脚本,但问题是它总是在其他部分进行,而不是报告一个文件。我在输入的目录中确实有文件。我做错了什么 我的要求是递归地访问目录中的每个文件,打开它并以字符串形式读取它。但逻辑的第一部分是失败的 #!/usr/bin/perl -w use strict; use warnings; use File::Find; my (@dir) = @ARGV; find(\&process_file,@dir); sub process_file { #print

我已经编写了以下perl脚本,但问题是它总是在其他部分进行,而不是报告一个文件。我在输入的目录中确实有文件。我做错了什么


我的要求是递归地访问目录中的每个文件,打开它并以字符串形式读取它。但逻辑的第一部分是失败的

#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find;

my (@dir) = @ARGV;
find(\&process_file,@dir);

sub process_file {
    #print $File::Find::name."\n";
    my $filename = $File::Find::name;
    if( -f $filename) {
        print " This is a file :$filename \n";
    } else {
        print " This is not file :$filename \n";
    }
}

$File::Find::name
给出相对于原始工作目录的路径。但是,除非您另有说明,否则将继续更改当前工作目录

使用
no\u chdir
选项,或者使用只包含文件名部分的
-f$\u
。我推荐前者

#!/usr/bin/perl -w
use strict; 
use warnings;
use File::Find;

find({ wanted => \&process_file, no_chdir => 1 }, @ARGV);

sub process_file {
    if (-f $_) {
        print "This is a file: $_\n";
    } else {
        print "This is not file: $_\n";
    }
}

这段代码对我来说似乎工作得非常好(XP上的ActiveState Perl 5.10)。你怎么称呼你的剧本?你所说的“但是逻辑的第一部分失败了”到底是什么意思?你在使用哪个平台?哪个perl版本?“我的要求是递归地访问目录中的每个文件,打开它并以字符串形式读取。但是逻辑的第一部分失败了。”我指的是逻辑的第一部分,访问目录中的每个文件。我的文件检查失败。@TopCoder:这是
which
的版本,如果
perl
路径中没有
perl
perl-V
,或者可能是
$(which perl)--version
$(which perl)-V
。对不起,这是正确的版本:perl,为x86_64-linux-thread-multiMy构建的v5.8.8错误!删除了虚假评论和+1。