2013-04-22 41 views
0

我解析了一個文件,然後我創建了多個輸出文件。我想將輸出文件保存在執行perl腳本的同一文件夾中的特殊目錄中。如果該目錄已經存在,則將該文件放在該目錄中。如果用戶沒有sudo權限,則在當前工作目錄中創建文件。如何將輸出文件保存在目錄中並打印輸出文件的名稱?

use strict; use warnings; 

sub main { 
    my $directory = "pwd/test.pl_output"; 

    unless(mkdir $directory) { 
     return 0; # unable to create directory (it either already exists 
         # or you don't have sudo privileges) 
    } else { 
     return $directory 
     } 
} 

if (my $PATH = main()){ 
    open(my $fh, '<', 'input.txt') or die $!; 
    open(my $output, '+>', $PATH.'/output.txt') or die $!; 
} else { # create the output file as usual 
    open(my $fh, '<', 'input.txt') or die $!; 
    open(my $output, '+>', 'output.txt') or die $!; 
} 

print "All done! Please look in \$output\n"; 

在腳本的末尾,我已經解析和處理的文件後,我想打印出以下shell提示符:

All done! Please look in 'output.txt' for the output. 

如果輸出文件是在新目錄,我想打印以下內容:

All done! Please look in 'output.txt' in the directory '~/path/to/output.txt' for the output. 

我的代碼不起作用。

+1

我不習慣perl,但是我在這裏有一個問題,在這個$ PATH中,'$ PATH/output.txt'會被替換爲'''裏面的值嗎? – abasu 2013-04-22 05:05:10

+0

abasu這是另一個好主意。單引號不會內插。我懷疑我們沒有看到實際的複製粘貼代碼。 – DavidO 2013-04-22 05:06:07

+0

我知道可以[在perl中創建一個目錄](http://www.caveofprogramming.com/perl/perl-mkdir-how-to-create-directories-in-perl/)。但我不知道我想要做的其他事情是否可能。 – cooldood3490 2013-04-22 05:21:06

回答

3

您現有的代碼有幾個嚴重的問題。一個是你在一個雙引號字符串中使用pwd。這是行不通的。你可能使用pwd裏面的反引號並捕獲輸出,但不是在雙引號字面。

另一個問題是,你的代碼的邏輯並沒有接近你想要如何正常降級目的地的描述的複雜性。

以下代碼片段將首先在可執行文件的目錄中查找名爲「special」的目錄。如果它不存在,它會嘗試創建它。如果創建失敗(可能是由於權限),它將接下來在用戶的當前工作目錄中查找名爲「special」的目錄。如果它不存在,它會嘗試創建它。如果失敗了,它會隨着描述性信息而死亡。

如果它經過這一點,那麼「特殊」目錄或者已經存在,或者已經沿着一條允許的路徑創建。接下來,打開文件。如果打開輸出文件失敗,我們會死。否則,繼續並可能寫入文件。然後關閉輸入和輸出文件。最後,打印可能找到輸出文件的路徑。

use strict; 
use warnings; 

use FindBin; 
use File::Path qw(make_path); 

my $special_dir = 'special'; 
my $filename = 'my_file.txt'; 


my $bin = $FindBin::Bin; 

my $path; 


if(not defined($path = get_path("$bin/$special_dir", "./$special_dir"))) { 
    die "Unable to find or create a suitable directory for output file."; 
} 

my $output_filename = "$path/$filename"; 


open my $in_fh, '<', 'input.txt' or die $!; 
open my $out_fh, '+>', $output_filename or die $!; 

# Do whatever it is you want to do with $in_fh and $out_fh.... 

close $out_fh or die $!; 
close $in_fh or warn $!; 

print "All done! Please look in $output_filename for the output.\n"; 



sub get_path { 
    my @tries = @_; 
    my $good_path; 
    for my $try_path (@tries) { 
    if(-e $try_path and -d _ and -w _) {   # Path exists. Done. 
     $good_path = $try_path; 
     last; 
    } 
    elsif(eval { make_path($try_path); 1; }) { # Try to create it. 
     $good_path = $try_path;      # Success, we're done. 
     last; 
    } 
    } 
                # Failure; fall through to 
                # next iteration. If no 
                # more options, loop ends 
                # with $path undefined. 
    return $good_path; 
} 

我正在使用Perl模塊FindBin來定位可執行文件。並且正在使用File :: Path來創建該目錄。

0

過去幾次,我發現最好使用perl腳本和Shell腳本來達到這個目的。 Perl腳本解析和shell腳本進行文件夾處理。用shell腳本輕鬆包含perl腳本。這將更容易和方便。

相關問題