Subject: | Odd number of elements in hashref because map and split bug |
Parse-Syslog-Mail-0.03
v5.8.5 built for i386-linux-thread-multi
RHEL 4 WS, Linux 2.6.9-5.0.5.EL, i686
MTAs sometimes return messages with commas in the text, (and sometimes results contain additional equal signs)
Take a look at this common result:
"stat=Deferred: 453 sorry, mailbox currently unavailable"
Chopping up the map on commas then produces the elements:
'stat=Deferred: 453 sorry' and 'mailbox currently unavailable'
When we then split on the equal sign, we get:
{
'stat' => 'Deferred: 453 sorry'
'mailbox currently unavailable' => undef
}
This patch finds commas with a word immediately followed by and equal sign and chops the map up before that word by replacing the comma with a tab character. The map is then produced using \t instead of ', '. The patch also limits the amount of splits that happen within each mapped element, in the case of a result string also containing an equal sign, to ensure equal hash elements.
*** /tmp/Mail.pm 2005-05-11 17:11:26.000000000 -0500
--- Mail.pm 2005-05-12 10:07:44.571806522 -0500
***************
*** 177,187 ****
$log->{text} =~ s/^\s*([^=]+)\s*$/status=$1/;
! my @fields = split ', ', $log->{text};
%mail = map {
s/,$//; s/^ +//; s/ +$//; # cleaning spaces
s/^stat=/status=/; # renaming 'stat' field to 'status'
! split /=/
} @fields;
$mail{id} = $id;
$mail{timestamp} = $log->{timestamp};
}
--- 177,193 ----
$log->{text} =~ s/^\s*([^=]+)\s*$/status=$1/;
! $log->{text} =~ s/collect: /collect=/;
! $log->{text} =~ s/([^\s]+),\s+([^\s]+)=/$1\t$2=/g;
!
! my @fields = split '\t', $log->{text};
%mail = map {
s/,$//; s/^ +//; s/ +$//; # cleaning spaces
s/^stat=/status=/; # renaming 'stat' field to 'status'
! split(/=/,$_,2);
} @fields;
$mail{id} = $id;
+ $mail{host} = $log->{host};
+ $mail{program} = $log->{program};
+ $mail{text} = $log->{text};
$mail{timestamp} = $log->{timestamp};
}