2011-10-03 73 views
1

可能重複:
How can I pass command-line arguments to a Perl program?perl的輸入多個文件

我有一個Perl腳本,我需要在命令行中運行,但也提供了三個輸入文件和一個輸出。我不想做一個STDIN,因爲我正在編寫一個包裝腳本來多次爲多個文件使用perl腳本。

所以,我現在這樣:

open (FICHIER, "<$file1") || die "file not found"; 
chomp($file1); 
@b=<FICHIER>; 

open (SPECIES, "<$file2"); 
$species=<SPECIES>; 
chomp($species); 

open (FILE3, "<$file3>"); 
$file3 = <FILE3>; 
chomp($file3); 

open (OUTFILE, ">$outfile"); 
chomp($outfile); 

我需要做這樣的事情:

perl的myscript.pl textfile1 textfile2 textfile3 OUTPUTFILE

我需要$文件1來表示textfile1, $ file2 for textfile2等

在此先感謝

馬克

回答

1

在主程序中,移位函數將關閉@ARGV的參數,這是命令行參數的列表。所以你可以說:

my $file1 = shift || die "Need 4 file names"; 
my $file2 = shift || die "Need 4 file names"; 
my $file3 = shift || die "Need 4 file names"; 
my $outfile = shift || die "Need 4 file names"; 

另外,「use strict」和「使用警告」;是很好的做法,並打開三個參數::

open my $FICHIER, '<', $file1 or die "$file1 - $!";