Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

To avoid dying, surround the call to XMLin with an eval statement like this:

my $ref; eval { $ref = $x->XMLin(...); print STDERR "\nHash dump:\n"; print STDERR Dumper($ref); # this only returns from the eval, not the function #make sure we only end up in ... or do {...} if we are dying return 1; } or do { #sometimes you can die but $@ is '' or undefined!!! my $err=$@ || "Something bad happened, but I don't know what!"; print STDERR "Invalid content in file: $@"; }

The above code has one problem though. If you have a lot of things you are going to do with $ref, you'll probably want to halt processing whenever you see a problem. Also if you put too much code in eval {...} you might have things that die and aren't due to invalid content, but rather to something you did with valid content after you put it in the XML file. You might prefer something like this:

sub processContent { # do initial processing ... my $ref; eval { $ref = $x->XMLin(...); return 1; #return from eval only } or do { my $err=$@ || "Something bad happened, but I don't know what!"; print STDERR "Invalid content in file: $@"; # doesn't make sense to continue # in a do {...}, this returns from the enclosing *subroutine* return 0; #FAILURE!!! } print STDERR "\nHash dump:\n"; print STDERR Dumper($ref); # do other stuff with $ref (surrounding individual statements # with eval {...} or do {...} as needed .... return 1; #SUCCESS! }

Note: I changed your output stream to STDERR. In almost all cases it is better to send error messages to STDERR rather than STDOUT (print "some string" sends output to STDOUT). That way you can keep your "real output" separate from your logging, debugging and error messages.

Best, beth

In reply to Re: XML::Simple - Invalid file content by ELISHEVA
in thread XML::Simple - Invalid file content by nagalenoj

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2018-05-23 22:08 GMT
    Find Nodes?
      Voting Booth?