gpt4 book ai didi

perl 严格引用错误

转载 作者:行者123 更新时间:2023-12-02 05:38:28 25 4
gpt4 key购买 nike

我收到以下 perl 错误。

Can't use string ("") as a symbol ref while "strict refs" in use at test173 line 30.

粘贴下面的代码。第 30 行是 open 语句。失败了在公开声明中。我有 use strict;use warnings; 在脚本。错误表示什么?怎么改代码解决这个错误。

my $file = 'testdata';
open($data, '<', $file) or die "Could not open '$file'\n";
print "file data id:$data\n";
@iu_data = <$data>;
$totalLineCnt = @iu_data;
print "total line cnt: $totalLineCnt". "\n";

最佳答案

请确保您之前没有为 $data 赋值。我只需三行即可重现您的问题:

use strict;
my $data = '';
open($data, '<', 'test.txt');

例如,您可以通过创建新范围来解决问题:

use strict;
my $data = '';
{
my $data;
open($data, '<', 'test.txt');
close($data);
}

或者,您可以在使用它之前取消定义 $data:

use strict;
my $data = '';
undef $data;
open($data, '<', 'test.txt');
close($data);

等等等等......

关于perl 严格引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com