您的代码似乎假定您正在传递文件句柄,而不是文件名。您需要打开文件并为其分配文件句柄。
# This doesn't work as $input contains a file name
GetOptions('input=s' => \$input,'output=s' => \$output);
# This doesn't work for two reasons:
# 1/ $input is a file name, not a filehandle
# 2/ You've omitted the file input operator
while ($input) {
...
}
你想要更像这样的东西:
# Get the file names
GetOptions('input=s' => \$input,'output=s' => \$output);
# Open filehandles
open my $in_fh, '<', $input or die "Can't open $input: $!";
open my $out_fh, '>', $output or die "Can't open $output: $!";
# Read the input file using a) the input filehandle and b) the file input operator
while (<$in_fh>) {
...
}
我也认为这里可能还有另一个问题。我不是 Windows 专家,但我认为您的文件名可能会被误解。尝试在命令行中反转斜杠:
perl myprogram.pl -input C:/inputfilelocation -output C:/outputfilelocation
或加倍反斜杠:
perl myprogram.pl -input C:\\inputfilelocation -output C:\\outputfilelocation
或者也许引用论点:
perl myprogram.pl -input "C:\inputfilelocation" -output "C:\outputfilelocation"