zoukankan      html  css  js  c++  java
  • perl Can't use string Cxxx) as a symbol ref while "strict refs" in use at XXXX.pl错误

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

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

    例如:

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

    运行之后是这个错误:

    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
    ";
    }
    open(TIME,">date.log") or die "$!
    ";
    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 文件句柄和函数不能使用相同的名字,以后再遇到类似的错误,注意检查一下文件名和分号

  • 相关阅读:
    257. Binary Tree Paths
    324. Wiggle Sort II
    315. Count of Smaller Numbers After Self
    350. Intersection of Two Arrays II
    295. Find Median from Data Stream
    289. Game of Life
    287. Find the Duplicate Number
    279. Perfect Squares
    384. Shuffle an Array
    E
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3668765.html
Copyright © 2011-2022 走看看