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_
???

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