perl Can't use string Cxxx) as a symbol ref while "strict refs" in use at XXXX.pl错误

时间:2022-06-01 19:10:58

今天写脚本遇到Can't use string ("bond2     Link encap:InfiniBand ") as a symbol ref while "strict refs" in use at test.pl line 的错误

google了一下,发现*有这样的错误,其中有一个是缺少一个分号导致的

例如:

open (FILE, "ifconfig | ");
while (<FILE>)
{
my $line = $_;
print $line
sleep ;
}

运行之后是这个错误:

Ifconfig is obsolete! For replacement check ip.
Can't use string ("bond2 Link encap:InfiniBand ") as a symbol ref while "strict refs" in use at test.pl line 15, <FILE> line 1

我仔细检查我自己的代码,并未发现缺少分号,运行之后依然报错

我的代码是:就是运行data命令,把结果写到data.log中

#!/usr/bin/perl
use strict;
sub TIME
{
my $time=readpipe("date");
return "$time\n";
}
open(TIME,">date.log") or die "$!\n";
print TIME &TIME;
close(TIME);

运行之后是这个样子

Can't use string (XXXXX) as a symbol ref while "strict refs" in use at test.pl line 8.

并非缺少分号,我又仔细看了,发现函数名和文件句柄名都是TIME,就把文件句柄名改成别的,然后脚本就可以正常运行

原来perl 文件句柄和函数不能使用相同的名字,以后再遇到类似的错误,注意检查一下文件名和分号