XML::Parser::Style::Subs

X

xhoster

The code in XML::parser::Style::Subs seems to be quite slow in some
situations, and also it silently eats any errors that might occur in
the call backs.

sub Start {
no strict 'refs';
my $expat = shift;
my $tag = shift;
my $sub = $expat->{Pkg} . "::$tag";
eval { &$sub($expat, $tag, @_) };
}

I've replaced it for my purposes with:

sub Start {
no strict 'refs';
my $sub = $_[0]->{Pkg} . "::$_[1]";
return unless defined &$sub;
&$sub(@_);
}

The sub End was also modified analogously.)

This is faster and propagates errors as expected. But it probably doesn't
work for AutoLoaded callbacks. I don't care about that for my current
purposes, but for the sake of general improvement, is there a way to
arrange for the code to be faster and propagate errors while still working
for AutoLoad?

Thanks,

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
B

Ben Morrow

Quoth (e-mail address removed):
The code in XML::parser::Style::Subs seems to be quite slow in some
situations, and also it silently eats any errors that might occur in
the call backs.

sub Start {
no strict 'refs';
my $expat = shift;
my $tag = shift;
my $sub = $expat->{Pkg} . "::$tag";
eval { &$sub($expat, $tag, @_) };
}

I've replaced it for my purposes with:

sub Start {
no strict 'refs';
my $sub = $_[0]->{Pkg} . "::$_[1]";
return unless defined &$sub;
&$sub(@_);
}

The sub End was also modified analogously.)

This is faster and propagates errors as expected. But it probably doesn't
work for AutoLoaded callbacks. I don't care about that for my current
purposes, but for the sake of general improvement, is there a way to
arrange for the code to be faster and propagate errors while still working
for AutoLoad?

You ought to be able to use $_[0]->{Pkg}->can($_[1]) instead of defined
&$sub: packages which use AUTOLOAD ought to provide a corresponding
->can. Any that actually use AutoLoader will.

Of course, now you've replaced an eval BLOCK with a method call, which
is probably slower :). TBH, I don't think AutoLoader is much used now
(the benefits are arguable), and a package using AUTOLOAD for clever
tricks would be better off using the XML::parser interface directly.

The right answer is to stop grubbing around in the symbol table and
write XML::parser::Style::Hashref, which takes a hashref with the same
keys as the subs in ::Subs.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top