Perl 很新,所以这里可能有一个非常明显的解决方案。我正在浏览一个日志文件,基本上将某些内容添加到哈希中,但我不断收到:
“在 logTest.pl 第 37 行第 9 行通过包“serv.int”(也许您忘记加载“serv.int”?)找不到对象方法“1339384721”。我已经初始化了这些变量和所有内容,所以我不明白为什么 perl 会抱怨......
#!/usr/bin/perl -w
use strict;
use warnings;
my $LOGFILE = '/Users/user/Desktop/logTest';
my $downTime = 0;
my $serviceName = 0;
my %downTimeHash = ();
open(LOGFILE, $LOGFILE) or die ("Couldn't open the file.");
foreach my $line (<LOGFILE>) {
chomp($line);
#Checks for 'STATE' lines down
if ($line=~/\s*;DOWN*/ && ($line=~/STATE:\s+([^;]+)/ || $line=~/ALERT:\s+([^;]+)/)) {
#Get time service went down
if ($line=~/\[(\d*)\]*/) {
$downTime = $1;
}
#Get service that went down
if ($line=~/STATE:\s+([^;]+)/ || $line=~/ALERT:\s+([^;]+)/) {
$serviceName = $1;
}
#Add service and down time to hash
%downTimeHash = ($serviceName->$downTime);
}
}
print "%downTimeHash \n";