It is indeed horrible that XML::Simple dies on you on bad XML (a module should never die).

Without wanting to sound defensive :-) ... Calling 'die' is Perl's mechanism for throwing an exception. Using 'eval' to catch exceptions should be part of your everyday programming style. If that's not how you're handling errors in say DBI then you need to read up on what you're missing.

Please don't perpetuate the myth that 'eval' is costly. If you eval a string then it is true that Perl's compiler must be reinvoked to parse the string - that cost may be significant in some circumstances. However if you use eval to wrap a code block then the code in that block is compiled during the initial parse and the overhead imposed by eval is comparable to the overhead of calling a subroutine.

In general, a module should not call 'die' though. In general, it should 'use Carp' and 'croak' on errors. This will give the user of the module a better indication of where in their code the problem is being triggered.

Here's a Perl one-liner I use for checking well-formedness from the command line:

perl -MXML::Parser -e "XML::Parser->new( ErrorContext => 3 )->parsefile(shift)" filename

In reply to Don't knock die (was Checking XML well-formedness) by grantm
in thread Checking XML well-formedness by uzzikie

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.