Im using XML::Twig to process a rather large XML document. have a look at the example below (moved from the scratchpad on advise below) It seams to Segfault no matter where I force it to exit the loop, or how.

Trouble here is that I HAVE to process the archive tag first, it has some information that is required for the rest of the code to work (shortened for example).

The alternative is of course to change the xml.. to

<archive>
    <important_tag info="here">
    <directory path="foo/"/>
    <directory path="foo/bar/"/>
    <directory path="foo/wibble/"/>
</archive>

Code follows....

#!/usr/bin/perl

use XML::Twig;

my $twig = new XML::Twig( TwigHandlers => { 
    archive => \&process_archive,

} );

open FILE, 'new.xml' or die ("arrrrrrrrrrgh");
read (FILE, $data, (-s 'new.xml'));
close FILE;

# dont work, note the file is 17MB of XML
#$twig->parse( $data );

# works
$twig->parse( \*DATA );

sub process_archive {
    my ($t, $elt) = @_;
   
    ## grab and process attributes

    my @children = $elt->children;
    my $count = 0;
    my $last = 0;        
    
    foreach ( @children ) {
        $count++;
        print "Processing Directory ".$count." of ".scalar @children."\n";
        last if ($count eq 2);
        
    }

}

__DATA__
<archive info="here">
    <directory path="foo/"/>
    <directory path="foo/bar/"/>
    <directory path="foo/wibble/"/>
</archive>

But it only does it with large docs, the small doc in the example works fine!

Some version info as requested. I know there is a newer version of XML::Twig out there, but Ive yet to try it.

OS: Linux - Fedora Core
Arcitecture : i386
Perl version : 5.8.1
XML::Twig ver : 3.17
XML::Parser ver : the one shipped with 5.8.1

In reply to XML::Twig segfaulting on large docs by Catharsis

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.