perl
2012-07-05 32 views 2 likes 
2

我得到了下面的perl错误。perl strict ref error

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"; 

回答

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); 

等等......

相关问题