0
我目前正在编写一个Perl程序,用于读取给定文件(命令行或硬编码),然后递归打印(并打开,如果扩展名为.bragi
)列出的文件和目录。例如:perl递归文件读取
~
hello.bragi
subdir/
~/subdir
check.bragi
其中
master.bragi:
~/hello.bragi
和
hello.bragi:
subdir/
和
check.bragi:
main.c
该计划将打开master.bragi
,见hello.bragi
上市,打开它找到一个d列出irectory,打开该目录,然后重复。
我现在有这样的代码:
#!/usr/bin/perl -w
use strict;
use File::Basename;
sub isdir {
return (-d $_[0]);
}
sub isfile {
return (-f $_[0]);
}
sub getfn {
my $path = $_[1];
my (undef, undef, my $ext) = fileparse($_[0], qr"\..*");
print "arg:\t".$path."\n";
if ($ext eq ".bragi") {
open FILE, "<", $path.$_[0] or die $!;
my @lines = <FILE>;
foreach my $line (@lines) {
chomp($line);
if (isfile($line)) {
print "file:\t".$path.$line."\n";
}
if (isdir($line)) {
print "DIR:\t".$line."\n";
opendir my ($dh), $path.$line or die "Filename does not exist: $!";
my @files = readdir $dh;
closedir $dh;
#print $files[0].":\t".$path.$line."/\n";
foreach my $f (@files) {
my $next = $path.$line."/";
getfn($f, $next);
}
}
}
}
}
getfn("master.bragi", "/home/tekknolagi/twentytwelve/fs/");
除非我得到这样No such file or directory at ./files.pl line 19, <FILE> line 3.
一些错误,而且我不完全知道我在做什么。思考?
预期输出(按顺序):
master.bragi
hello.bragi
check.bragi
main.c
已编辑,我仍然在努力研究'File :: Find'。评论我的新代码? – tekknolagi
你为什么说“被黑客”? – tekknolagi
因为我改变了它 - 我拿了你原来的东西并改变了它(砍死它)。这些变化并不一定像我想的那样精美,但你至少应该认识到它的可识别性。 –