in reply to RFC: Acme::ExceptionEater
This seems to do the automatic wrapping properly. It just needs some clean-up and modulification, and it should be ready to ruin the day of anyone foolish enough to use it.
use strict; # Symbol table experiments. use Data::Dumper; #use Win32::SerialPort; # All entries in symbol table are uniquely identified by a typeglob. #print join "\n", # $::{'main::'}, # $main::{'main::'}, # $main::main::{'main::'}, # $main::main::main::{'main::'}, # ; my %OMIT; BEGIN { my @OMIT = qw( *main::UNIVERSAL:: ); @OMIT{@OMIT} = (); } wrap_destroy($_) foreach find_namespaces( 'main::' ); eval { my $f = Foo->new(); die 'adsfasdf'; }; eval { my $f = Foo::Foo->new(); die 'adsfasdf'; }; eval { my $f = Foo::Foo::Foo->new(); die 'adsfasdf'; }; eval { my $f = Foo::Foo::Bar->new(); die 'adsfasdf'; }; sub find_namespaces { my $seen = ref($_[0]) eq 'HASH' ? shift : {} ; my @names = @_; my @results; foreach my $name ( @names ) { no strict 'refs'; foreach my $entry ( keys %{$name} ) { next unless $entry =~ /::$/; my $typeglob = ${$name}{$entry}; next if $$seen{$typeglob}++; find_namespaces( $seen, $typeglob ); } } return keys %$seen ; } sub has_destroy { my $namespace = shift; no strict 'refs'; if ( exists ${$namespace}{DESTROY} ) { print "\t$namespace has DESTROY\n"; } } sub inherits_destroy { my $namespace = shift; $namespace =~ s/^\*(main::)?//; $namespace =~ s/::$//; return UNIVERSAL::can( $namespace, 'DESTROY' ); } sub wrap_destroy { my $namespace = shift; no warnings; no strict 'refs'; my $eater = $namespace . "DESTROY"; return if exists $OMIT{$namespace}; my $sub; if( has_destroy( $namespace ) ) { my $orig = \&{ ${$namespace}{DESTROY} }; $sub = sub { print "$eater Ate $namespace\n"; my @caller = caller(1); my @this = caller(1); $orig->(@_); eval{}; }; } elsif ( inherits_destroy( $namespace ) ) { print "$namespace inherits DESTROY\n";; } else { print "$namespace has no DESTROY\n";; $sub = sub { print "$eater Swallowed $namespace\n"; eval{}; } } *{ "$eater" }= $sub; } package Foo; sub new { bless {}, __PACKAGE__; } sub DESTROY { 1; } package Foo::Bar; sub new { bless {}, __PACKAGE__; } sub DESTROY { 1; } package Foo::Foo; sub new { bless {}, __PACKAGE__; } sub DESTROY { 1; } package Foo::Foo::Foo; sub new { bless {}, __PACKAGE__; } use base 'Foo::Foo'; package Foo::Foo::Bar; sub new { bless {}, __PACKAGE__; }
TGI says moo
|
|---|