in reply to XML::Twig and Eval

Please supply a complete, self-contained example that exhibits the problem you show.

I can't replicate your problem using the following code. Most likely, you're calling ->text on an undefined value somewhere else in your program:

use strict; use XML::Twig; warn XML::Twig->VERSION; my $twig = XML::Twig->new(); $twig->parse(<<'XML'); <addr> <city/> </addr> XML my $res = eval { $twig->first_child('address')->first_child('city')->text; }; print "Got " . $res; __END__ 3.48 at tmp.pl line 4. Use of uninitialized value $res in concatenation (.) or string at tmp. +pl line 16. Got

Replies are listed 'Best First'.
Re^2: XML::Twig and Eval
by Anonymous Monk on Mar 28, 2016 at 09:28 UTC
    small correction,the XML is :
    <address> <city/> </address>
    and not <addr>

    taking off my SIG{__DIE__} handler which was calling explicitly exit made it continue

    Does Eval call the DIE handler? but what if I want to continue when the code is wrapped in eval and die in all other occasions? I'm probably missing something fundamental here

      The fundamental thing is to not have a $SIG{__DIE__} handler at all, or at least, to not call exit in there. Also see $^S in perlvar and the discussion of it there relating to %SIG.

      Basically, what you've done is bad design and the sooner you go away from a $SIG{__DIE__}, the sooner your headaches will go away.

      IIRC, in your example, there's no text on the element "city", so it will be undef.

      Also eval seems to indicate that if you're using SIG{__DIE__} within your code, you'll want a local handler inside the eval.