This I like though due to the fixed stack we have to work with I cannot use Perl later than 5.8.8 and also cannot use LibXML 1.70. I appreciate that is a whole topic of conversation in itself but those are the boundaries. I do appreciate the help though as this is very helpful in getting me going with Perl.
However, I've done a little bit with the code to make it work with 5.8.8 and I'm almost there but currently only outputting subnet and I need gateway, cidr and netmask too.
...
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new('GET', $url);
my $res = $ua->request($req);
my $parser = XML::LibXML->new();
sub smart_match
{
my ($target, $param) = @_;
if (ref $param eq 'Regexp')
{
return ($target =~ qr/$param/);
}
else
{
return ($target eq $param);
}
}
my $xml = $parser->parse_string( $res->content() );
my @results;
foreach my $dict ($xml->findnodes('//dict'))
{
my $key = $dict->getAttribute('key');
#next unless $key eq [qw( 'subnet', 'gateway', 'cidr', 'netmas
+k' ) ];
next unless smart_match( $key, qw( subnet gateway cidr netmask
+ ) );
print Dumper($key);
push @results, {} if $key eq 'subnet';
my $value = $dict->findvalue('.//value');
$results[-1]{ $key } = "$value";
}
print Dumper \@results;
Which outputs:
$VAR1 = 'subnet';
$VAR1 = 'subnet';
$VAR1 = 'subnet';
$VAR1 = 'subnet';
$VAR1 = 'subnet';
$VAR1 = [
{
'subnet' => '10.11.14.0'
},
{
'subnet' => '10.11.15.0'
},
{
'subnet' => '10.15.8.0'
},
{
'subnet' => '10.7.17.224'
},
{
'subnet' => '10.11.24.0'
}
];
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.