在将parameter passing给Perl脚本的时候,并没有打开文件

这是脚本用于比较两个.csv文件,并在results.xls中写入差异

我的Perl脚本(名称:cmp.pl)是:

#!C:\Perl\bin use Spreadsheet::WriteExcel; use Spreadsheet::WriteExcel::Utility; my $Wb = Spreadsheet::WriteExcel->new('results.xls'); my $s1 = $Wb->add_worksheet('res'); open(FILE1, "< ARGV[0]") || die "Cannot open $ARGV[0]\n"; open(FILE2, "< ARGV[1]") || die "Cannot open $ARGV[1]\n"; @file1 = < FILE1>; @file2 = < FILE2>; my $format = $Wb->add_format(); my $format1 = $Wb->add_format(); $format->set_bg_color('red'); $format1->set_bg_color('yellow'); for $i (0 .. $#file1) { $line1 = $file1[$i]; $line2 = $file2[$i]; if (!($line1 eq $line2)) { @x = split(/\,/, $line1); @y = split(/\,/, $line2); for $j (0 .. $#x) { if ((($x[$j] != $y[$j]) || (!($x[$j] eq $y[$j])))) { $s1->write($i, $j, $y[$j], $format); } else { $s1->write($i, $j, $y[$j], $format1); } } } else { @x = split(/\,/, $line1); $s1->write_row($i, 0, \@x); } } $Wb->close(); close(FILE1); close(FILE2); 

我在cmd Promt中传递了参数(文件)

\perl>cmp.pl t1.csv t2.csv

输出:显示无法打开

打开文件并将其读入数组@file1@file2应该如下所示

 open my $fh, '<', $ARGV[0] or die qq{Cannot open "$ARGV[0]": $!\n}; my @file1 = <$f1>; open $fh, '<', $ARGV[1] or die qq{Cannot open "$ARGV[1]": $!\n}; my @file2 = <$f2>; close $fh; 

最后两个close电话应该被删除。

你应该改变你的开放线 – 你已经忘记在ARGV[0]之前放置一个$ (你正在访问一个数组元素):

 use strict; use warnings; 

你可以使用这个:

 open my $fh, '<', $ARGV[0] or die $!; 

或这个:

 my $file = $ARGV[0]; open my $fh, '<', $file or die $!;