This one is baffling me. I have an XML document I am trying to process with XML::Twig and I am getting an error where I think it should not.

Here is the setup:
the xml I am trying to parse looks like this
<document> <record> <attachment>data</attachment> </record> </document>

my $twig = XML::Twig->new(twig_handlers=>{record => \&process_record, attachment => \&process_attachment});
$twig->parse($xml);

then in the handlers
sub process_attachment() { my ($twig, $attach) = @_; do some data processing .... $attach->purge; } sub process_record() { my ($twig, $record) = @_; do some data processing .... $record->purge; }

This produces an error: Can't locate object method "purge" via package "XML::Twig::Elt" at <line number>

Where line number points to $attach->purge;

but in the examples given in the perldoc: "You can also purge it if you don't need to output it (if you are just extracting some data from the document for example). The handler will be called again once the next relevant element has been parsed."

$attach->flush does seem to work but I don't want to print it, so why doesn't $attach->purge work?

If I use $twig->purge it kills the whole record, not just the attachment.

The problem I am trying to address is that a record in the xml can have several very large attachments. I want to parse each attachment (loading it into an array) and then purge the xml of just the attachment from memory, leaving the rest of the record in tact until it can be processed by its handler.


In reply to broken twig? by gregaryh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.