Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: XML::Parser Style=>Subs and undefined subroutines

by rkg (Hermit)
on Oct 01, 2003 at 01:27 UTC ( [id://295489]=note: print w/replies, xml ) Need Help??


in reply to XML::Parser Style=>Subs and undefined subroutines

Ack! Even worse:
use strict; use XML::Parser; my $p1 = new XML::Parser(Style => 'Subs'); $p1->parse('<foo id="me">Hello World</foo>'); sub foo { my ($expat, $tag, %a) = @_; print "foo before death\n"; die "i cannot die"; print "foo after death\n"; } sub foo_ { my ($expat, $tag, %a) = @_; print "i am alive and running foo_\n"; }
On my system the output is
foo before death i am alive and running foo_
  • Why isn't the die fatal?
  • Why did  foo_ get called?
???

Replies are listed 'Best First'.
Re: Re: XML::Parser Style=>Subs and undefined subroutines
by antirice (Priest) on Oct 01, 2003 at 01:46 UTC

    Check out XML::Parser::Style::Subs within the Start and End subs, the following is executed:

    my $sub = $expat->{Pkg} . "::$tag"; eval { &$sub($expat, $tag, @_) };

    All that happens is the dies are captured into $@ and the Start and/or End subs return undef.

    Update: Btw, for the behavior you seek, try this instead:

    package XML::Parser::Style::DieSubs; sub Start { no strict 'refs'; my $expat = shift; my $tag = shift; my $sub = $expat->{Pkg} . "::$tag"; $sub->($expat,$tag,@_) if defined &{$sub}; } sub End { no strict 'refs'; my $expat = shift; my $tag = shift; my $sub = $expat->{Pkg} . "::${tag}_"; $sub->($expat,$tag) if defined &{$sub}; } 1;

    It doesn't die if the sub doesn't exist, though. It does, however, allow your subroutines to die. If you wish for it to die if the subroutines don't exist, this is fairly trivial given the above code.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://295489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found