Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

XML::Twig newbie question

by slugger415 (Beadle)
on Sep 18, 2012 at 01:29 UTC ( #994164=perlquestion: print w/ replies, xml ) Need Help??
slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlers, I am still learning XML::Twig and have a question. I have a couple of event handlers set up thusly, parsing a list of filenames stored in @files:

foreach my $file (@files){ # set up the XML parser: my $twig= XML::Twig->new( twig_handlers => { topicref => \&topicref_processing, xref => \&topicref_processing }, ); $twig->parsefile($file); $twig->purge; }

And I have the handler set up something like this:

sub topicref_processing { my($twig, $topicref) = @_; my($atts) = $topicref->atts(); if($$atts{'keyref'}){ ### associate this keyref with $file } }

What I want to do is, if the handler finds a keyref attribute in the xref or topicref element, store that in a hash with the $file name as the key, something like $keyrefs{$file}{$$atts{'keyref'}} = 'keyref'.

What I can't figure out is, how to pass the name in $file to the event handler, or conversely, how to associate the found attribute back to the $file. I'm probably missing something obvious, but can't puzzle it out.

Any suggestions would be greatly appreciated.

Scott

Comment on XML::Twig newbie question
Select or Download Code
Re: XML::Twig newbie question
by Anonymous Monk on Sep 18, 2012 at 02:19 UTC

    What I can't figure out is, how to pass the name in $file to the event handler, or conversely, how to associate the found attribute back to the $file. I'm probably missing something obvious, but can't puzzle it out. Any suggestions would be greatly appreciated.

    You're missing closures, examples in XML::Twig traversing tree and storing in an array, XML::Twig n00b, Tutorials: Closure on Closures

    for my $file ( ... ){ my $filename = $file; my %filnameKeyref; my $topicref = sub { ... $filenameKeyref{ $filename }; return; }; my $twig= XML::Twig->new( ... => $topicref ); $twig->...; }

    Although you ought to write that as

    for my $file ( ... ){ my $complexAssociations = FooAssoc( $file ); WalkAndTalk( $complexAssociations ); }

      Another way would be to make the subroutine curried (that is expecting to get the parameters separately, not all at once):

      foreach my $file (@files){ # set up the XML parser: my $twig= XML::Twig->new( twig_handlers => { topicref => topicref_processing($file), xref => topicref_processing($file) }, ); $twig->parsefile($file); $twig->purge; } sub topicref_processing { my ($file) = @_; return sub { my($twig, $topicref) = @_; my($atts) = $topicref->atts(); if($$atts{'keyref'}){ ### associate this keyref with $file } } }

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

        Another way would be to make the subroutine curried (that is expecting to get the parameters separately, not all at once):

        Sure, you could write your closure that way ( the schönfinkeling way), but it gains you nothing in this example, it just takes a little more memory

        "Curried?" sounds delicious! :-) Never heard that phrase before.

        Thank you, this is a simple solution that works perfectly for me.

        Scott

      Thank you for the suggestions; I will study the examples and try to understand them. A bit over my head at first sight.

      Scott

Re: XML::Twig newbie question
by remiah (Hermit) on Sep 18, 2012 at 15:03 UTC

    There is another way ,which you find it in tutorial of twig. Please see "sub make{" and its handler registration.

    my $twig= XML::Twig->new( twig_handlers => { topicref => sub{ topicref_processing(@_, $filenam +e);}, ); sub topicref_processing{ my($twig,$elt, $filename)=@_; ####you can get twig and elt here. }
    I wonder why links to XML::Twig documents have so much troubles...?

        Thanks.
        Updated my bookmark.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994164]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2014-10-23 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls