提问人:fthomas 提问时间:10/20/2021 更新时间:10/20/2021 访问量:146
在关闭的文件句柄上打印:使用警告 - Perl [重复]
Print on Closed File handle : Use Warnings- Perl [duplicate]
问:
我正在尝试打开文件句柄,打印文件句柄并关闭它。
use strict;
use warnings;
open( $fh, ">>", "$filename");
print $fh $mesg."\n";
close $fh;
现在,当我尝试编译此文件时,它会抛出错误:print() on closed filehandle $fh
只有在我添加了使用警告后,才出现此问题。
提前致谢。
答:
2赞
choroba
10/20/2021
#1
您应该始终检查 open 的返回值:
open my $fh, '>>', $filename or die "Can't open $filename: $!";
或者,您可以使用 autodie,这样即使您不检查其返回值,它也会失败。open
评论
0赞
fthomas
10/20/2021
感谢您的回复,我试过了这个,它抛出“无法打开<位置>”
0赞
choroba
10/20/2021
没有别的了吗?没有冒号?另外,有没有一个名为 ?<location>
0赞
fthomas
10/21/2021
是的,它说<位置> : 21
0赞
choroba
10/21/2021
你用的是什么操作系统?
0赞
fthomas
10/21/2021
我正在使用 ssh 连接到 Linux 服务器
评论