Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: sub that finds ancestor elements

by GrandFather (Sage)
on Sep 13, 2012 at 02:06 UTC ( #993344=note: print w/replies, xml ) Need Help??

in reply to sub that finds ancestor elements

XML::Simple isn't. The immediate problem is a need to use ForceArray. However your code can stand a fair bit of tidying up generally. There is no need to use prototypes. You should avoid overloading identifiers (r used for the name of two different subs especially is nasty). Identifiers generally should indicate their purpose. Consider:

#!/usr/bin/perl use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); use XML::Simple qw(:strict); run(); sub run { my $xml = XMLin( <<'END', ForceArray => ['parameters'], KeyAttr => ['declaratio +n_name', 'name']); <classes name="Panoply::BAR"> <public_methods> <members name="BAR" const="no" kind="function" protection="publi +c" static="no" virtualness="non_virtual" volatile="no" > <parameters declaration_name="pciReg" type="Register::Ptr" /> </members> </public_methods> <enums> <members name="ObjectState" kind="enum" protection="public" stat +ic="no" virtualness="non_virtual"> <values name="NEW"> </values> <values name="REFRESHED"> </values> <values name="DIRTY"> </values> </members> </enums> </classes> END my @list; my $sub = sub {push(@list, shift(@_) . ": " . join(" => ", @_))}; findPath($sub, $xml, "pciReg"); findPath($sub, $xml, "DIRTY"); findPath($sub, $xml, "parameters"); print join("\n", @list), "\n"; } sub findPath { my ($sub, $xmlFrag, $match, @path) = @_; return unless $xmlFrag; if (ref($xmlFrag) =~ /HASH/) { for my $key (sort keys %$xmlFrag) { if ($key !~ /$match/) { findPath($sub, $xmlFrag->{$key}, $match, @path, $key); } else { $sub->($match, @path); } } } elsif (ref($xmlFrag) =~ /ARRAY/) { for my $fragIdx (0 .. $#$xmlFrag) { unless ($xmlFrag->[$fragIdx] =~ /$match/) { findPath($sub, $xmlFrag->[$fragIdx], $match, @path, $fragIdx + 1); } else { $sub->($match, @path); } } } return; }


pciReg: public_methods => members => parameters DIRTY: enums => members => values parameters: public_methods => members
True laziness is hard work