当向Perl脚本传递参数时,它不会打开文件

时间:2021-05-11 00:29:46

This is script is used to compare two .csv files and write difference in results.xls

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

my perl script(name:cmp.pl) is:

我的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);

I passed the arguments(files) in cmd promt like

我通过了cmd的参数(文件)

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

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

output:its showing cannot open

输出:显示无法打开

2 个解决方案

#1


1  

The code where you open the files and read them into arrays @file1 and @file2 should look like this

打开文件并将其读入数组@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;

and the two close calls at the end should be removed.

在最后的两个接近的呼叫应该被删除。

#2


0  

You should change your open lines - you've forgotten to put a $ before the ARGV[0] (you're accessing an array element):

您应该更改开放行——您忘记在ARGV[0]之前输入$(您正在访问数组元素):

use strict;
use warnings;

You could use this:

你可以用这个:

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

Or this:

或:

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

#1


1  

The code where you open the files and read them into arrays @file1 and @file2 should look like this

打开文件并将其读入数组@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;

and the two close calls at the end should be removed.

在最后的两个接近的呼叫应该被删除。

#2


0  

You should change your open lines - you've forgotten to put a $ before the ARGV[0] (you're accessing an array element):

您应该更改开放行——您忘记在ARGV[0]之前输入$(您正在访问数组元素):

use strict;
use warnings;

You could use this:

你可以用这个:

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

Or this:

或:

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