Perl:"使用未初始化的值"虽然值已初始化

时间:2015-07-29 08:13:22

标签: perl shell initialization perl-module

虽然我已经在开始时正确初始化了所有变量,但Perl正在重新发出警告。 这是我生成随机单播mac-address的代码:

our $OCTATE_MAX = 255; #Max value stored in 8-bit field
our $octate1 = 0;
our $octate2 = 0;
our $octate3 = 0;
our $umac_serial = "";
sub get_umac_serial
{
    # Can not use sprintf as there is an issue with Igloo-JT
    #return "00:" . sprintf ("\00", int(rand(100))) . ":" . sprintf("\00",int(rand(100)));
     my $first = int(rand(99));
     my $second = int(rand(99));

     return "00:" . "$first" . ":" . "$second";
}
sub get_umac_address
{
    $octate1 = $octate1 + 1;
    $octate1 = int(rand(99));
    $octate2 = int(rand(99));
    $octate3 = int(rand(99));
    if ($octate1 >= $OCTATE_MAX)
    {
        $octate2 = $octate2 + 1;
        $octate1 = 0;
    }
    elsif ($octate2 >= $OCTATE_MAX)
    {
        $octate1 = 0;
        $octate2 = 0;
        $octate3 = $octate3 + 1;
    }
    elsif ($octate3 >= $OCTATE_MAX)
    {
        $umac_serial = get_umac_serial();
        print("Unicast mac-serial # $umac_serial\n");
        $octate1 = 0;
        $octate2 = 0;
        $octate3 = 0;
    }
    $umac_serial = get_umac_serial();
    #my $mac_addr = $umac_serial . ":" . sprintf("\00", $octate3) . ":" . sprintf("\00", $octate2) . ":" . sprintf("\00",$octate1);

    my $mac_addr = $umac_serial . ":" . "$octate3" . ":" . "$octate2" . ":" . "$octate1";

    print("MAC ADDR - $mac_addr\n");
    return $mac_addr;
}
return $result;
#exit;
}   ####### End of mac_addr

虽然值已正确初始化,但perl会抛出以下错误:

Jul 29 01:14:25 [WARN ] perl: Use of uninitialized value in addition (+) at /var/lib/igloo_jt/explorer/2015/7/1251/mac_addr.pm line 77. Use of uninitialized value in addition (+) at /var/lib/igloo_jt/explorer/2015/7/1251/mac_addr.pm line 77. 
Jul 29 01:14:25 [WARN ] perl: Use of uninitialized value in numeric ge (>=) at /var/lib/igloo_jt/explorer/2015/7/1251/mac_addr.pm line 81. Use of uninitialized value in numeric ge (>=) at /var/lib/igloo_jt/explorer/2015/7/1251/mac_addr.pm line 81. MAC ADDR - 00:74:53:58:33:0 
Jul 29 01:14:25 [WARN ] perl: Use of uninitialized value in numeric ge (>=) at /var/lib/igloo_jt/explorer/2015/7/1251/mac_addr.pm line 81. Use of uninitialized value in numeric ge (>=) at /var/lib/igloo_jt/explorer/2015/7/1251/mac_addr.pm line 81. 
MAC ADDR - 00:51:68:64:69:0

这段代码有什么问题?

2 个答案:

答案 0 :(得分:1)

我敢打赌,在程序的顶部,您没有在问题中显示的部分,您的主程序代码正在调用一个或多个子程序。

当您使用myour声明变量时,初始化不会在编译时发生。只有在正常的程序流程到达语句时才会发生。

如果我的预感是正确的,你可以通过在对任何使用变量的函数的调用之前将声明移动到程序的顶部来解决这个问题。或者您可以编写显式的编译时初始化,如下所示:

our $octate1;
our $octate2;
our $octate3;
BEGIN
{
    $octate1 = $octate2 = $octate3 = 0;
}

答案 1 :(得分:0)

我们无法真正回答您的问题,因为您的代码不是77行。启用strictwarnings也会有所帮助。

但是我可以建议你做一些比你需要的更复杂的事情 - 如果你想要生成一个随机的十六进制字符串mac地址:

print join ( ":", map { sprintf("%02X", int (rand(  256))-1) } (0..5) );

或者当您指示需要00前缀时:

print join ( ":", "00", map { sprintf("%02X", int (rand(  256))-1) } (0..4) );

我不确定你对sprintf的问题是什么 - 它是工作的工具 - 但是如果这不起作用还有其他转换选项(你可能必须编写自己的'dectohex'函数:

sub dec2hex_str {
    my ($value) = @_;
    my @conv = ( 0 .. 9, "A" .. "F" );
    my $result = '';
    while ($value) {
        my $mod = $value % 16;
        $value -= $mod;
        $value /= 16;
        $result = $conv[$mod] . $result;
    }
    if ( length($result) < 2 ) {
        $result = "0" . $result;
    }
    return $result;
}