我解析了一個文件,然後我創建了多個輸出文件。我想將輸出文件保存在執行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.
我的代碼不起作用。
我不習慣perl,但是我在這裏有一個問題,在這個$ PATH中,'$ PATH/output.txt'會被替換爲'''裏面的值嗎? – abasu 2013-04-22 05:05:10
abasu這是另一個好主意。單引號不會內插。我懷疑我們沒有看到實際的複製粘貼代碼。 – DavidO 2013-04-22 05:06:07
我知道可以[在perl中創建一個目錄](http://www.caveofprogramming.com/perl/perl-mkdir-how-to-create-directories-in-perl/)。但我不知道我想要做的其他事情是否可能。 – cooldood3490 2013-04-22 05:21:06