2011-11-23 117 views
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 

回答

3

一個問題是,你不使用的核心模塊File::Find。這旨在使目錄遍歷更容易。

另一個問題是,你有use strict;註釋掉。

另一個問題是,您不會爲getfn()的參數創建my變量。至少這是非常規的;使用好的變量名稱可以更容易理解代碼。

我收回了以前有關File::Find的評論。這裏,似乎工作腳本的黑客版本:

mkdir subdir 
echo hello.bragi > master.bragi 
echo subdir > hello.bragi 
echo main.c > subdir/check.bragi 
echo hello > subdir/main.c 

命令的輸出是:

file: /Users/jleffler/tmp/soq/hello.bragi 
DIR: /Users/jleffler/tmp/soq/subdir 
file: /Users/jleffler/tmp/soq/subdir/main.c 

#!/usr/bin/perl -w 

use strict; 
use File::Basename; 
use constant debug => 0; 

sub isdir { 
    return (-d $_[0]); 
} 

sub isfile { 
    return (-f $_[0]); 
} 

my $level = 0; 

sub getfn { 
    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"; 
       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--; 
} 

getfn("master.bragi", $ENV{PWD}); 

我在當前目錄這樣創造了一個測試環境

+0

已編輯,我仍然在努力研究'File :: Find'。評論我的新代碼? – tekknolagi

+0

你爲什麼說「被黑客」? – tekknolagi

+1

因爲我改變了它 - 我拿了你原來的東西並改變了它(砍死它)。這些變化並不一定像我想的那樣精美,但你至少應該認識到它的可識別性。 –