lunchlady55 has asked for the wisdom of the Perl Monks concerning the following question:

I'm very lost. I am trying to do the following:

I have an XML file from Nagios. Here's the structure:

1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 <NAGIOS> 3 <DATASOURCE> 4 <TEMPLATE>check_bigip_pool_connection</TEMPLATE> 5 <IS_MULTI>0</IS_MULTI> 6 <DS>1</DS> 7 <NAME>cur</NAME> 8 <UNIT></UNIT> 9 <ACT>2198</ACT> 10 <WARN></WARN> 11 <WARN_MIN></WARN_MIN> 12 <WARN_MAX></WARN_MAX> 13 <WARN_RANGE_TYPE></WARN_RANGE_TYPE> 14 <CRIT></CRIT> 15 <CRIT_MIN></CRIT_MIN> 16 <CRIT_MAX></CRIT_MAX> 17 <CRIT_RANGE_TYPE></CRIT_RANGE_TYPE> 18 <MIN></MIN> 19 <MAX></MAX> 20 </DATASOURCE> 21 <DATASOURCE> 22 <TEMPLATE>check_bigip_pool_connection</TEMPLATE> 23 <IS_MULTI>0</IS_MULTI> 24 <DS>2</DS> 25 <NAME>max</NAME> 26 <UNIT></UNIT> 27 <ACT>5400</ACT> 28 <WARN></WARN> 29 <WARN_MIN></WARN_MIN> 30 <WARN_MAX></WARN_MAX> 31 <WARN_RANGE_TYPE></WARN_RANGE_TYPE> 32 <CRIT></CRIT> 33 <CRIT_MIN></CRIT_MIN> 34 <CRIT_MAX></CRIT_MAX> 35 <CRIT_RANGE_TYPE></CRIT_RANGE_TYPE> 36 <MIN></MIN> 37 <MAX></MAX> 38 </DATASOURCE> ...

Much more follows. The above section is the part I care about. I want to get data from the <ACT> tags associated with the <NAME> "cur".

I'm reading it in with XML::Simple with the following code:

1 #!/usr/bin/env perl 2 3 4 use Data::Dumper; 5 use XML::Simple; ... 9 my $xs1 = XML::Simple->new(); 10 my $file = $ARGV[0]; 11 my $doc = $xs1->XMLin($file, forcearray => 1); 12

When I use Dumper($datasource), I get this:

$VAR1 = { 'NAGIOS_NOTIFICATIONRECIPIENTS' => {}, ... 'NAGIOS_HOSTACTIONURL' => '/nagios/pnp/index.php?host=ahostn +ame.somedomain.com', 'DATASOURCE' => [ { 'MIN' => {}, 'TEMPLATE' => 'check_bigip_pool_connection +', 'CRIT_MAX' => {}, 'MAX' => {}, 'UNIT' => {}, 'WARN_MAX' => {}, 'NAME' => 'cur', 'WARN_MIN' => {}, 'IS_MULTI' => '0', 'DS' => '1', 'WARN' => {}, 'CRIT_MIN' => {}, 'ACT' => '2198', 'CRIT' => {}, 'CRIT_RANGE_TYPE' => {}, 'WARN_RANGE_TYPE' => {} }, ...

Next, I ran this:

20 foreach my $datasource ($doc->{DATASOURCE}){ 21 22 print Dumper($datasource->[1]->{NAME}->[0]); 23 24 my $element = scalar(@datasource); 25 26 print "Element = $element\n"; 27 for(my $element=0; $element < scalar(@datasource); $element++){ 28 print $datasource[$element]; 29 } 30 }

and as far as I understand, datasource should be an array of hashes. But $element is 0, before I use it in the for loop. Output:

$VAR1 = 'max'; Element = 0

Now here's the weird part where I decided to ask for help. When I change the print statement to

 22   print Dumper($datasource[1]->{NAME}->[0]);

(notice the lack of '->' after $datasource) it changes my output to

$VAR1 = undef; Element = 2 HASH(0x10e2ef0)

  1. Why would changing the print statement change my output? Am I doing an assignment operation and I just don't realize it?
  2. How do I get the ACT value for all DATASOURCEs with the NAME "cur"?
Any help would be appreciated.

Thank You.

Replies are listed 'Best First'.
Re: Strange Behavior with XML::Simple when pulling info out of an XML file
by kcott (Archbishop) on Nov 27, 2010 at 00:20 UTC

    There are apparently a number of common mistakes made with this module: XML::Simple STRICT MODE will detect those for you.

    This mode has proven useful to others in the past, e.g. XML::Simple Error.

    -- Ken

Re: Strange Behavior with XML::Simple when pulling info out of an XML file
by roboticus (Chancellor) on Nov 26, 2010 at 23:16 UTC

    lunchlady55:

    I bet if you used strict and warnings, you'd have a message from perl telling you about the undeclared variable @datasource. I'm guessing that since you're getting 0, telling you it's empty. You're using a different variable ($datasource) in your for loop.

    ...roboticus

      OK, if anyone else stumbles upon this page, here's what I came up with...Apparently, $datasource is a reference to an array. So you can't just change the $ to an @. You have to use curly braces like this to read into the reference:

      $element = scalar(@{$datasource});

      My code now reads:

      1 #!/usr/bin/env perl 2 3 use strict; 4 use warnings; 5 #use IO::Socket; 6 use Data::Dumper; 7 use XML::Simple; 8 my $xs1 = XML::Simple->new(); 9 my $file = $ARGV[0]; 10 my $doc = $xs1->XMLin($file, forcearray => 1); 11 foreach my $datasource ($doc->{DATASOURCE}){ 12 my $length = scalar(@{$datasource}); 13 print $length ."\n"; 14 foreach my $element (@{$datasource}){ 15 print Dumper($element); 16 # my $element = scalar(@datasource); 17 # print "Element = $element\n"; 18 # for(my $element=0; $element < scalar($datasource); $element++){ 19 # print $datasource[$element]; 20 } 21 }

      Each $element is a reference to a hash now. I'll have to de-reference those to access them as well, I'm sure.

        In most languages, you can only use an identifier in one context - not so in Perl! You can have $x (scalar), @x (array), %x (hash), sub x{...} all in the same program. So $x is a completely unrelated thing to @x. As you have correctly surmised, the value of an array evaluated in a scalar context is the length of that array.

        $element = scalar(@{$datasource});
        $element = @$datasource; #same thing $element is a scalar so explicit conversion not required.
        $x < @$datasource #also scalar context
        You only need the extra curly braces when there is a subscript involved - so deference applies to the whole thing.

Re: Strange Behavior with XML::Simple when pulling info out of an XML file
by Jenda (Abbot) on Nov 27, 2010 at 23:11 UTC

    If there was just one DATASOURCE tag with a specific name you could use code like this:

    use strict; use warnings; no warnings 'uninitialized'; use XML::Rules; my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => 'content', DATASOURCE => 'by NAME', NAGIOS => 'pass no content', }, ); my $data = $parser->parse($file); #use Data::Dumper; #print Dumper($data); print $data->{cur}{ACT};
    If there are more it's a bit more complicated as there is no builtin rule you could use and it's harder to get at the values you want:
    my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => 'content', DATASOURCE => sub { my ($tag,$attrs) = @_; return '@'.$attrs->{NAME} => $attrs; }, NAGIOS => 'pass no content', }, ); my $data = $parser->parse($file); #use Data::Dumper; #print Dumper($data); foreach my $datasource (@{$data->{cur}}) { print $datasource->{ACT}, "\n"; }
    Of course if you do know you will not need the other data in the file you can ignore it and simplify the datastructure:
    my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => '', 'NAME,ACT' => 'content', DATASOURCE => sub { my ($tag,$attrs) = @_; return '@'.$attrs->{NAME} => $attrs->{ACT}; }, NAGIOS => 'pass no content', }, ); my $data = $parser->parse($file); #use Data::Dumper; #print Dumper($data); print join( ", ", @{$data->{cur}}), "\n";

    HTH, Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Thank you for your help. I've gotten it working now.