http://www.perlmonks.org?node_id=1002596


in reply to How to return two and more values by parsing XML with XML::Rules?

Sometimes its simpler to use variables in an outer scope than to go through contortions:
my (@am_keys, @lk_keys); my @rules = ( AM_Key => sub { push @am_keys, $_[1]{_content}; return }, LK_Key => sub { push @lk_keys, $_[1]{_content}; return }, _default => undef, ); my $xr = XML::Rules->new( rules => \@rules );
  • Comment on Re: How to return two and more values by parsing XML with XML::Rules?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to return two and more values by parsing XML with XML::Rules?
by Jenda (Abbot) on Nov 08, 2012 at 07:56 UTC

    Or, if you really are against using globals, use the $parser->{pad} to hold your data:

    my @rules = ( AM_Key => sub { push @{$_[4]->{pad}{am_keys}}, $_[1]{_content}; retu +rn }, LK_Key => sub { push @{$_[4]->{pad}{lk_keys}}, $_[1]{_content}; retu +rn }, Outpatient_Services => sub { LK_Keys => $_[4]->{pad}{lk_keys}, AM_Ke +ys => $_[4]->{pad}{am_keys}}, _default => undef, ); my $xr = XML::Rules->new( rules => \@rules );

    With this $xr->parse() returns a HoA containing the array of the AM_Keys and the array of LK_Keys.

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

Re^2: How to return two and more values by parsing XML with XML::Rules?
by vagabonding electron (Curate) on Nov 07, 2012 at 12:23 UTC
    Thank you very much for this!