Hi, The script below combines an XML Parse of a number of files in a directory, extracting the contents and inserting into a mysql database.

Whats happening is when an unrecognised encoding comes in XML::Parser bombs out as expected. The rest of the files are processed fine, the db inserts work, and the script ends with a nice messy segfault, though no core dump.

Am I not handling my die's correctly or is it something else ? I've currently 'handled' it by using LibXML instead!!

XML::Simple ( 1.08 ) Perl (5.6.1) XML::Parser (2.30) DBI (1.14), and DBD::mysql (2.0419).

use strict; use DBI; use XML::Simple; use Data::Dumper; my $dbh = DBI->connect( 'DBI:mysql:emailb:127.0.0.1', 'user', 'pass', { RaiseError => 1, PrintError => 1 } ) +; die "db connect failed $dbh" unless (ref $dbh eq 'DBI::db'); my $xdir = '/var/tmp/xmlfeed/'; opendir( DIRH, $xdir) or die "cant open $xdir $!"; my @files = grep { /xml$/ } readdir DIRH; closedir DIRH; print "Total files : ", scalar @files, "\n"; my $inc = 0; FILES : for (@files) { my $xmlfile = $xdir . $_; $inc++; print "Processing file number : $inc : $xmlfile \n"; my $struct; my $parser = new XML::Simple( forcearray => 1 ); eval { $struct = $parser->XMLin( $xmlfile ); }; if ($@) { print "XML file : $xmlfile invalid ", $@; next; }; undef $@; my $sth = $dbh->prepare(' insert into category_name values ( ?, ? +)' ); for ( @{ $struct->{'Story'} } ) { my $hashref = $_; eval { $sth->execute( @{ $hashref->{'cat_id'} } ); $sth->finish; }; if ($@) { print "category name insert failed : $@ : skipping \n", Du +mper ($hashref ), "\n------------\n"; next; } undef $@; } $struct = undef; } $dbh->disconnect;

In reply to eval not catching XML Parser die by agoth

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.