I am parsing large XML files (60GB +) with XML :: Twig and using it in an OO (Moose) script. I use the twig_handlers parameter to parse elements as soon as they are read in memory. However, I'm not sure how I can handle the Element and Twig.
Before I used Moose (and OO in general), my script looked like this (and worked):
my $twig = XML::Twig->new( twig_handlers => { $outer_tag => \&_process_tree, } ); $twig->parsefile($input_file); sub _process_tree { my ($fulltwig, $twig) = @_; $twig->cut; $fulltwig->purge;
And now I would do it like that.
my $twig = XML::Twig->new( twig_handlers => { $self->outer_tag => sub { $self->_process_tree($_); } } ); $twig->parsefile($self->input_file); sub _process_tree { my ($self, $twig) = @_; $twig->cut;
The thing is, now I see that I am missing the fulltwig . I realized that in the first version, different from OO, cleaning will help save memory: get rid of fulltwig as soon as I can. However, when using OO (and in order to rely on explicit sub{} inside the handler), I donβt see how I can clear the entire branch, because the documentation says that
$ _ is also set by the element, so it's easy to write inline handlers as
para => sub { $_->set_tag( 'p'); }
So, they are talking about the Element that you want to process, but not the most complete. So how can I remove this if it is not passed to the subroutine?
xml perl xml-twig
Bram vanroy
source share