Producing a hash of values from Parse::RecDescent...

D

DJ Stunks

Hello all,

I'm taking my first crack at using Pastor Conway's Parse::RecDescent.
My grammar works (after some hacking around) but I'm not sure how to
get my information into a hash in the way I'd like it.

Here's a copy of a short but complete script which demonstrates what
I'm trying to accomplish. Below is an example of the structure I'd
like returned once the parser is finished.

I tried to use <autotree> but I've hardly used objects at all, and, in
any event, I want to end up with all the variables in an array.

Does anyone have any insight as to how to capture this data as desired?
My next step is to buy Advanced Perl Programming (2nd Ed)...

Thanks in advance,
-jp

=========== Script ===========

#!/usr/bin/perl

use strict;
use warnings;

use Parse::RecDescent;

my $grammar = q{
<autotree>

header : config_line(s?) 'OIDs(Variables):' variable_line(s?) /\z/

config_line : config_var /=/ config_value
config_var : /\w+/
config_value : /\S+/

variable_line : /Var-\d+ =/ oid <skip:'\('> variable_name
oid : /[.\d]+/
variable_name : /([^)]+)\)/
};

my $parser = Parse::RecDescent->new($grammar);

my $header = do {local $/; <DATA>};

if (my $header_obj = $parser->header($header) ) {
print "Valid Header\n";

use Data::Dumper;
print Dumper $header_obj;
}
else {
print "Invalid Header\n";
}

__DATA__
NodeName=XYZ_10.91.224.12
NodeAliasName=XYZ12-Headquarters
Category=Category.0.12
ConfName=Category.0.12
OIDs(Variables):
Var-1 = 1.3.6.1.4.1.1.2(IpAddress)
Var-2 = 1.3.6.1.4.1.1.16(PktsReceived)
Var-3 = 1.3.6.1.4.1.1.15(PktsRetransmitted)
Var-4 = 1.3.6.1.4.1.1.14(PktsSentSuccess)
Var-5 = 1.3.6.1.4.1.1.27(totalEgressBytes)
Var-6 = 1.3.6.1.4.1.1.22(totalForwardPktsDropped)
Var-7 = 1.3.6.1.4.1.1.26(totalIngressBytes)
Var-8 = 1.3.6.1.4.1.1.23(totalReversePktsDropped)
Var-9 = 1.3.6.1.4.1.1.25(totalEgressBytes)
Var-10 = 1.3.6.1.4.1.1.24(totalngressBytes)

=========== Desired Output ===========

$VAR1 = {
'Category' => 'Category.0.12',
'ConfName' => 'Category.0.12',
'NodeAliasName' => 'XYZ12-Headquarters',
'NodeName' => 'XYZ_10.91.224.12',
'Variables' => [
{
'name' => 'PktsReceived',
'oid' => '1.3.6.1.4.1.1.2'
},
{
'name' => 'PktsRetransmitted',
'oid' => '1.3.6.1.4.1.1.15'
},
{
'name' => 'PktsSentSuccess',
'oid' => '1.3.6.1.4.1.1.14'
},
{
'name' => 'totalEgressBytes',
'oid' => '1.3.6.1.4.1.1.27'
},
{
'name' => 'totalForwardPktsDropped',
'oid' => '1.3.6.1.4.1.1.22'
},
{
'name' => 'totalIngressBytes',
'oid' => '1.3.6.1.4.1.1.26'
},
{
'name' => 'totalReversePktsDropped',
'oid' => '1.3.6.1.4.1.1.23'
},
{
'name' => 'totalEgressBytes',
'oid' => '1.3.6.1.4.1.1.25'
},
{
'name' => 'totalngressBytes',
'oid' => '1.3.6.1.4.1.1.24'
}
]
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top