【发布时间】:2015-12-04 09:50:32
【问题描述】:
我有一个关于我在github 上找到的代码的问题,并根据我的需要进行了更改。它通常工作正常,但我收到一些让我担心的警告。
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
Use of uninitialized value in numeric ge (>=) at ./check_puppetdash line 192.
第 192 行如下所示:
189 # Gather values for status details
190 my $details = '';
191 foreach (@interests) {
192 if ($nodes{$_} >= $thresholds_warning{$_}){
193 $details = $details . $_ . " = " . $nodes{$_} . "; ";
194 }
195 }
我将它固定到上面定义的哈希 %nodes:
# Parse the markup, loading up anything that matches into the %nodes hash
my %nodes;
foreach my $line(split('\n', $response->decoded_content)) {
if($line=~m/<a href="\/nodes\/(\w+)">(\d+)<\/a>/) {
$nodes{$1} = $2;
}
}
#Gather perfomance data
foreach my $interest(@interests) {
$np->add_perfdata(
label => $interest,
value => defined($nodes{$interest}) ? $nodes{$interest} : 0,
uom => undef,
);
}
我使用了现有脚本中的那部分。 当我在脚本中的任何位置打印 %nodes 的内容时,我得到一个空行。
我在 perl 方面不是很有经验,希望能得到一些提示。
PS:我也联系了原代码的开发者。
【问题讨论】:
-
HTML
a标签上的正则表达式匹配可能不起作用。在 %nodes 代码中添加print "$line\n";以查看内容是什么。可以是任意数量的东西(大写的A、标签中的空格等)。这就是为什么通常不建议使用正则表达式解析 HTML/XML 的原因。 -
或者,查看您正在解析的源 HTML。在这里提供一个示例也会有所帮助。