perl严格的ref错误

时间:2012-07-05 00:07:35

标签: perl

我收到以下perl错误。

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

粘贴下面的代码。第30行是公开声明。它失败了 在公开声明中。我有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";

1 个答案:

答案 0 :(得分:5)

请确保您之前没有为$ 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);

Etc等......

相关问题