1

I have written a perl function that executes a command and parses the command output. The command will provide the output mentioned below. In the function call i pass a number between 1 to 5 as arguments . '1' correspond to lan, 2 corresponds to wan, '3' corresponds to name and so on. ( see the output below) . for example if 1 is passed as an argument in the function call the expected output is '0' ( 1 = lan and value of lan = 0 ) . When i execute the script im not getting the expected output. null is returned.Any suggestions?

command output 1 :

[
    {
       "lan" : 0,           #1
       "wan" : 0,           #2
       "name" : "XYZ",      #3 
       "packets" : 0,       #4
       "bytes" : 0          #5
    }
]

Function call

$self->{'stats_obj'} = Statistics->new( ip => "ip addr")

    my $result = $self->{'stats_obj'}->statistics_get('1');
       INFO('statistics:' . $result );

Function:

sub statistics_get{
    my ($self, $option)= @_;

    my $result = $self->_get_hyd_tc();

    return $result->{$option};

}

sub _get_hyd_tc {

    my ($self)  = @_;

    my $opt;
    my %result;
    my $line;

    my $cmd     = 'cmd goes here';

    $self->execute($cmd);

    my $count =0;

    foreach my $line (  $self->output() ) {
    chomp $line;

    if ( $line =~ /(Requested table doesn't.*)/i ){

        ERROR('table doesnt exist'  . $line)

    }

    if ($line =~ /(.*)/) {

        $opt = $1;
        $count = 0;

   }
     elsif ( $line =~ /(.*)/) {

        my $key = $1;
        my $value = $2;
        $result{$opt}{++$count} = $value;
       }
    }

    return \%result;

}
2
  • 1
    Your elsif in _get_hyd_tc will never be entered. Why are you using the same condition in both the if and the elsif? And why are you using a condition that will match any string? .* means match zero or more characters. Commented May 19, 2015 at 21:22
  • 1
    Are the comments in the input in the real input or did you put them in? If you put them in, that's a JSON-format and your problem can be solved without any parsing on your part as the JSON module comes with perl. Commented May 19, 2015 at 21:59

1 Answer 1

0

You are approaching this the wrong way.

Your snippet you give is JSON. So really - your best bet by far is to process it as JSON and not try and parse it yourself.

Something like this:

use strict;
use warnings;

use JSON;
use Data::Dumper;

my $command_output = '[
    {
       "lan" : 0,           
       "wan" : 0,           
       "name" : "XYZ",       
       "packets" : 0,       
       "bytes" : 0          
    }
]';


my $json_ob = decode_json( $command_output );
print Dumper \$json_ob;

print $json_ob -> [0] -> {'name'};

Those [] in your text denotes an array. usually that means multiple elements. You could iterate those elements, but as you've just got one, accessing it via [0] does the trick.

Now, you could if you really want map the attributes from 'words' to numeric, but ... there isn't really any need.

But to answer your question - why is null returned - it's because:

if ($line =~ /(.*)/) {

Will always evaluate as true - zero or more of anything.

And therefore you will never run the second elsif loop and so this never happens:

    $result{$opt}{++$count} = $value;

And so you never have anything other than an empty array to return.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.