在 Perl 中返回数组
我正在 Perl 中开发一个递归文件查找函数,该函数应该返回一个文件名数组。然而,当我尝试打印它们时,我只得到 0
。我做错了什么?
use strict;
use File::Basename;
use constant debug => 0;
sub isdir {
return (-d $_[0]);
}
sub isfile {
return (-f $_[0]);
}
my $level = 0;
#my @fns = ();
sub getfn {
my @fns = ();
my($file, $path) = @_;
my (undef, undef, $ext) = fileparse($file, qr"\.[^.]+$");
$level++;
print "-->>getfn($level): $file : $path\n" if debug;
print "arg:\t$file\t$path ($ext)\n" if debug;
if ($ext eq ".bragi") {
open my $FILE, "<", "$path/$file" or die "Failed to open $path/$file: $!";
my @lines = <$FILE>;
close $FILE;
foreach my $line (@lines) {
chomp($line);
my $fullpath = "$path/$line";
print "---- $fullpath\n" if debug;
if (isfile($fullpath)) {
#print "file:\t$fullpath\n";
push(@fns, $fullpath);
getfn($line, $path);
}
elsif (isdir($fullpath)) {
#print "DIR:\t$fullpath\n";
opendir my ($dh), $fullpath or
die "$fullpath does not exist or is not a directory: $!";
my @files = readdir $dh;
closedir $dh;
foreach my $f (@files) {
getfn($f, "$fullpath");
}
}
}
}
print "<<--getfn($level)\n" if debug;
$level--;
#print @fns;
return @fns;
}
foreach my $f (<*>) {
#print "fn: ".$f."\n";
my (undef, undef, $ext) = fileparse($f, qr"\.[^.]+$");
if ($ext eq ".bragi") {
print &getfn($f, $ENV{PWD})."\n";
}
}
I'm working on a recursive file-finding function in Perl that's supposed to return an array of filenames. What happens, though, is when I try to print them, I just get 0
. What am I doing wrong?
use strict;
use File::Basename;
use constant debug => 0;
sub isdir {
return (-d $_[0]);
}
sub isfile {
return (-f $_[0]);
}
my $level = 0;
#my @fns = ();
sub getfn {
my @fns = ();
my($file, $path) = @_;
my (undef, undef, $ext) = fileparse($file, qr"\.[^.]+$");
$level++;
print "-->>getfn($level): $file : $path\n" if debug;
print "arg:\t$file\t$path ($ext)\n" if debug;
if ($ext eq ".bragi") {
open my $FILE, "<", "$path/$file" or die "Failed to open $path/$file: $!";
my @lines = <$FILE>;
close $FILE;
foreach my $line (@lines) {
chomp($line);
my $fullpath = "$path/$line";
print "---- $fullpath\n" if debug;
if (isfile($fullpath)) {
#print "file:\t$fullpath\n";
push(@fns, $fullpath);
getfn($line, $path);
}
elsif (isdir($fullpath)) {
#print "DIR:\t$fullpath\n";
opendir my ($dh), $fullpath or
die "$fullpath does not exist or is not a directory: $!";
my @files = readdir $dh;
closedir $dh;
foreach my $f (@files) {
getfn($f, "$fullpath");
}
}
}
}
print "<<--getfn($level)\n" if debug;
$level--;
#print @fns;
return @fns;
}
foreach my $f (<*>) {
#print "fn: ".$f."\n";
my (undef, undef, $ext) = fileparse($f, qr"\.[^.]+$");
if ($ext eq ".bragi") {
print &getfn($f, $ENV{PWD})."\n";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的主要问题是这样的一行:
并没有真正做任何事情。它找到子目录中的所有文件,但随后完全丢弃它们。您需要将其返回值合并到外部调用的
@fns
中。第二个问题是:
强制返回的数组被视为标量,因此它打印数组元素的数量,而不是数组元素的内容。你可能想要这样的东西:
The main problem here is that a line like this:
doesn't really do anything. It finds all the files in the subdirectory, but then it completely discards them. You need to incorporate its return value into your outer call's
@fns
.A second problem is that this:
forces the returned array to be treated as a scalar, so it prints the number of array elements rather than the contents of the array elements. You probably want something like this:
当您递归调用 getfn() 时,您永远不会将返回的数组分配给任何内容。您唯一的任务是:
在函数的顶部,这就是返回的内容。
You never assign the returned array to anything when you call
getfn()
recursively. Your only assignment is:at the top of the function, and that's what is getting returned.