考虑添加
use strict;
use warnings;
到脚本的顶部。这些语句将有助于捕获 Perl 默默忽略的错误。此外,考虑在您的open
调用中添加错误检查:很可能,它实际上并没有打开。我会这样写:
use strict;
use warnings;
open(OUTPUT, "> Filter2/filter2.out")
or die "Can't open filter2.out: $!";
open(ERROR, "> Filter2/filter2.err")
or die "Can't open filter2.err: $!";
print OUTPUT "Beginning\n";
print ERROR "Beginning\n";
例如,通过添加我得到的严格和警告:
print() on closed filehandle OUTPUT at .\printer.pl line 6.
print() on closed filehandle ERROR at .\printer.pl line 7.
唔...!
通过添加错误检查,我得到:
PS C:\dev> perl .\printer.pl
Can't open filter2.out: No such file or directory at .\printer.pl line 4.
啊!看,我没有文件夹。添加文件夹后,一切正常。你可能会发现类似的东西。
最后,您可能还应该使用现代的词法文件句柄。这有助于捕获其他错误(例如重复使用的句柄名称。)因此,最终脚本将如下所示:
use strict;
use warnings;
open(my $output, ">", "Filter2/filter2.out")
or die "Can't open filter2.out: $!";
open(my $error, ">", "Filter2/filter2.err")
or die "Can't open filter2.err: $!";
print $output "Beginning\n";
print $error "Beginning\n";
中提琴!现在您可以准确地看到问题出在哪里,因为它失败了,并确保您稍后编写的其他库或代码不会意外地干扰您的文件句柄。