PERL and Anonymous Hashes and Debugging

PERL has a seemingly infinite number of libraries out there.  One that I find extremely helpful and which I just happened to stumble upon is the Data::Dumper one.  For anyone that has used Perl references and anonymous hashes, you know how tough debugging can be.   But Data::Dumper is so cool.  Just use the library and then print the main reference to see the whole tree:


#!/usr/bin/perl
use Data::Dumper;
use warnings;
use diagnostics;
use strict;

my ($nextline,$HOST,$COMPONENT,$RESTOFLINE,$PHASE,$INTERVAL,$FLOWS,$OVERFLOW);
my ($masterHash);

$masterHash = {};
while ( defined($nextline=) )
{
        chomp ($nextline);
        if ( $nextline =~ m/^[A-Za-z]+\s+\d+\s+\d+:\d+:\d+\s+([A-Za-z0-9_]+)\s+\[\d+\]\s+([A-Za-z0-9_]+):(.*)$/ )
        {
                $HOST = $1;
                $COMPONENT = $2;
                $RESTOFLINE = $3;
                $HOST = lc($HOST);
                $COMPONENT = lc($COMPONENT);
                if ( $RESTOFLINE =~ m/\s+\[\w+\]\s+\[\d+\]\s+([0-9])\s+:\s+Interval\s+([0-9]+):\s+([0-9]+)\s+flows.*CO,\s+([0-9]+)\s+OF/ )
                {
                        $PHASE = $1;
                        $INTERVAL = $2;
                        $FLOWS = $3;
                        $OVERFLOW = $4;
                        $masterHash->{$INTERVAL}->{$COMPONENT}->{$PHASE}->{"FLOWS"} = $FLOWS;
                        $masterHash->{$INTERVAL}->{$COMPONENT}->{$PHASE}->{"OVERFLOWS"} = $OVERFLOW;
#                        print "$HOST\t$COMPONENT\t$PHASE\t$INTERVAL\t$FLOWS\t$OVERFLOW\n";
                }
                elsif ( $RESTOFLINE =~ m/\s+\[\w+\]\s+:\s+Interval\s+([0-9]+):\s+([0-9]+)\s+flows.*CO,\s+([0-9]+)\s+OF/ )
                {
                        $PHASE = 0;
                        $INTERVAL = $1;
                        $FLOWS = $2;
                        $OVERFLOW = $3;
                        $masterHash->{$INTERVAL}->{$COMPONENT}->{$PHASE}->{"FLOWS"} = $FLOWS;
                        $masterHash->{$INTERVAL}->{$COMPONENT}->{$PHASE}->{"OVERFLOWS"} = $OVERFLOW;
#                        print "$HOST\t$COMPONENT\t$PHASE\t$INTERVAL\t$FLOWS\t$OVERFLOW\n";
                }
        else
        {
            print "Failed to match ($nextline)\n";
        }
        }
    else
    {
        print  "Failed to match ($nextline)\n";
    }
}
print Dumper($masterHash);

Comments

Popular posts from this blog

Xubuntu Home Server on Dell XPS 13 9370

Cygwin + syslog-ng

Installing Fedora 21 on a (late 2014) Mac Mini