Perl IO::文件因错误“无法在 script.pl 行 LINE 上使用未定义的值作为符号引用”而死亡 - 无法写入文件

问题描述 投票:0回答:1

尝试打开文件句柄以写入文本,但脚本始终失败并出现错误:

Can't use an undefined value as a symbol reference at C:/Users/USER/Documents/script.pl line 207, <GEN1> line 20001.
 at C:/Users/USER/Documents/script.pl line 207, <GEN1> line 20001.

示例代码:

my $fileo = 'C:\Users\USER\Documents\report.txt';

# Open output file handle
my $fho = IO::File->new($fileo, 'w');
say $fho "THIS FILE: $fileo";
$fho->close();

我已经检查了

Perldocs
MetaCPAN
关于模块
IO:File
但我看不出语法有任何问题。

有什么想法吗?

file perl io undefined handle
1个回答
0
投票

打开文件时出错。

my $fho = IO::File->new( $fileo, 'w' )
   or die( "Can't create `$fileo`: $!\n" );

额外的层没有用处。

open( my $fho, '>', $fileo )
   or die( "Can't create `$fileo`: $!\n" );
© www.soinside.com 2019 - 2024. All rights reserved.