Camel with FurThis is a silly mistake that can be easily avoided.

I was working on a Perl script that grabbed network traffic information from my ntop server and reported on it. ntop provides an API that specifically supports Perl. Although I was seeing the data coming from ntop, I wasn't able to work with it at all and I kept getting this error:

Odd number of elements in hash assignment

This was really annoying me! Here's a snippet of the code:

#!/usr/bin/env perl

use LWP::Simple;
use strict;
use warnings;

my $ntopDump = get("http://ntop.slaptijack.com/dumpData.html?language=perl&key=$ip");
my %ntop;

if ( $ntopDump eq "" ) {
  print STDERR "No ntop data returned for $ip\\n";
} else {
  %ntop = eval($ntopDump);
}

I couldn't see anything wrong with this code, but it kept throwing the error and not populating %ntop. The error wouldn't show up if warnings were turned off, but that didn't change the outcome at all. After a bunch of debugging, it finally dawned on me that the problem was related to the way ntop sends the data to my script.

ntop sends a Perl hash that should be evaled into your own hash. It looks like this:

%ntopHash =(
);

My problem is that %ntopHash was not properly declared and use strict was not liking that at all. So, I simply declared %ntopHash and everything worked just fine. Here's what the final code looked like.

#!/usr/bin/env perl

use LWP::Simple;
use strict;
use warnings;

my $ntopDump = get("http://ntop.slaptijack.com/dumpData.html?language=perl&key=$ip");
my %ntop;
my %ntopHash;

if ( $ntopDump eq "" ) {
  print STDERR "No ntop data returned for $ip\\n";
} else {
  %ntop = eval($ntopDump);
}

I hope that helps someone in the future.