in reply to Accessing 2nd level elements in XML::Simple and using the data in an SNMP call

After fixing up your sample data (it's missing a closing bracket & brace) and shortening it, your code runs fine:

my $data = { 'PairedDevices' => [ { 'PairName' => 'Chestnut', 'MetricSeverity' => '34', 'MinPercent' => '20', 'HostOwningGroup' => 'AIX Team', 'StatusAlertThisPair' => 'Y', 'StatusSeverity' => '31', 'HostedEnvironment' => 'Test', 'MetricAlertThisPair' => 'Y', 'MaxPercent' => '80', 'HostedApplication' => 'IEOR', 'HostOS' => 'AIX' } ] }; foreach my $e (@{$data->{PairedDevices}}) { print "Pairname is: " . $e->{PairName} . "\n "; print "Hosted Apps are: " . $e->{HostedApplication} . "\n" ; print "HostOS is: " . $e->{HostOS} . "\n"; }

Output:

Pairname is: Chestnut Hosted Apps are: IEOR HostOS is: AIX

You don't really want to comment out use strict. Really, you don't. Removing strict doesn't fix problems, it just hides them. The problems lurk, hidden, until the time you least want them to reappear.

Replies are listed 'Best First'.
Re^2: Accessing 2nd level elements in XML::Simple and using the data in an SNMP call
by wruehl (Acolyte) on Jul 31, 2007 at 13:05 UTC
    I only commented out the use strict because I was testing random SNMP functions and didn't want to have to go and declare them in each iteration. Once I know what I'm going to be doing(which variable names and the like) I'll put them in the header and uncomment the use strict.

      I don't think strict does what you think it does. There's no need to pre-declare variables or functions (subs) in Perl. However, strict will complain about you using global variables without explicit package names. All you have to do is use lexically-scoped variables. It does this for very good reasons.

      Or, in other words, when you first use a variable, use my:

      my $xml = new XML::Simple;

      When we advocate use strict;, we do so with good reason and as a result of vast experience. Personally -- I've been screwed over a few times because I was too lazy to work with strict in place. And vicariously -- we've all helped folks solve problems that would have been entirely avoided with the use of strict and warnings.

      So please, use strict; use warnings; -- it really will help you learn good Perl more quickly!

      <radiant.matrix>
      Ramblings and references
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
        Thanks for all the help. The final code is posted below, well a base functionality skeleton is at least, including the trial SNMPget call. It returns a hash instead of a value, but at least now I know that the communication syntax is correct. If anyone has hints as to how to get a value instead of a hash out of the call, that would be appreciated. Here's the code now, with "use strict" enabled.
        #!/usr/bin/perl -w use strict; #modules used use XML::Simple; use Data::Dumper; use Net::SNMP; our ($data, $xml, $e, $Host, $OID, $port, $value, $IPAddressA, $sessio +n, $error); #create object $xml = new XML::Simple; # MIB Hard value taken from parameters of the DC1 fab port pairs scrip +t appended a 1 to test port #1 $OID = ".1.3.6.1.4.1.289.2.1.1.2.3.1.1.152.1"; #checking against PD110 as a test run will probably be read from an XM +L input file in the future $Host = "10.2.125.110"; # read the XML file $data = $xml->XMLin("FabPortPairs.xml"); #print Dumper($data); foreach $e (@{$data->{PairedDevices}}) #Do actions that require use of XML data in this loop possibly #call SNMP data gathering and reporting { print "Pairname is: " . $e->{PairName} . "\n "; print "Hosted Apps are: " . $e->{HostedApplication} . "\n" ; print "HostOS is: " . $e->{HostOS} . "\n"; $IPAddressA = $e->{DeviceA}->{IPAddress} ; print "Var name for IpAddressA is " . $IPAddressA . "\n"; } ##test SNMP code print "SNMP test run \n"; ($session,$error) = Net::SNMP->session(Hostname => $Host, Community => "public"); die "session error: $error" unless ($session); $value = $session->get_request($OID); die "request error " . session->error unless (defined $value); $session->close; print "port info: " . $value . "\n";