Top*_*der 11 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 $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";
}
}
Run Code Online (Sandbox Code Playgroud)
ike*_*ami 20
$File::Find::name给出相对于原始工作目录的路径.但是,File :: Find会不断更改当前工作目录,除非您另行说明.
使用该no_chdir选项,或使用-f $_仅包含文件名部分的选项.我推荐前者.
#!/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";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25775 次 |
| 最近记录: |