没有见过这样的事情,但也许子程序属性可能会让你有所作为?
这是一个小的概念证明,使用Attribute::Handlers
抛出异常处理程序.pm
package ThrowsExceptionHandler;
use Modern::Perl;
use Attribute::Handlers;
our @subs;
sub ThrowsException :ATTR(CODE) {
push @subs, {
package => $_[0],
symbol => $_[1],
subname => *{$_[1]}{NAME},
referent => $_[2],
attr => $_[3],
data => $_[4],
phase => $_[5],
filename => $_[6],
linenum => $_[7],
};
}
sub does_throw {
my ($class, $subname) = @_;
(grep { $_->{subname} eq $subname } @subs) ? 1 : 0;
}
1;
例子.pl
use Modern::Perl;
use base qw(ThrowsExceptionHandler);
sub baz :ThrowsException {
die "Throws error";
}
sub foo {
warn "warning only";
}
say ThrowsExceptionHandler->does_throw( 'baz' ); # => 1
say ThrowsExceptionHandler->does_throw( 'foo' ); # => 0
也许(混合)PPI
,Perl::Critic
和/或Padre
可以适应使用这样的东西?
/I3az/