为什么我被警告在以下Perl脚本中使用未初始化的值?

Zen*_*net 2 perl xml-parsing

我正在尝试减少此列表中打印的端口数:

A.B.C.D 80,280,443,515,631,7627,9100,14000

对我最感兴趣的那些:

A.B.C.D 80,515,9100

为此,我使用这段代码:

foreach (@ips_sorted) {
  print "$_\t";
  my $hostz = $np->get_host($_);
    my $port = 0;
    my $output = 0;
    $port = $hostz->tcp_ports('open');
  if ($port == 80 || $port == 445 || $port == 515 || $port == 9100) {
    $output =  join ',' ,$port;  
  } 
  print $output;

  print "\n";
}
Run Code Online (Sandbox Code Playgroud)

我可能不需要说,它不起作用.我明白了:

A.B.C.D 0

Use of uninitialized value $port in numeric eq (==) at parse-nmap-xml.pl line **(line with if).

dax*_*xim 5

很可能,表达式$hostz->tcp_ports('open')返回了undef,而不是像你预期的那样.

  • 请改用`if(defined $ port)`. (3认同)
  • 萨曼莎,现在这是一个不同的问题.请[打开一个新问题](http://stackoverflow.com/questions/ask),并在那里发布一些代码以帮助调试; 方法`get_host`和`tcp_ports`很有趣,需要`@ipps_sorted` /一些示例数据的内容. (2认同)