By the way, if that BEGIN block will end up being use statement, use subs is not needed. For example,
override.pm:
use strict; use warnings; package override; sub import { my ($class, $sub_name, $sub) = @_; my $pkg_name = caller(); my $pkg_sym = _pkg_symtab($pkg_name); delete $pkg_sym->{$sub_name}; if ($sub) { no strict 'refs'; *{"${pkg_name}::$sub_name"} = $sub; } } sub _pkg_symtab { my ($pkg) = @_; my $p = \%::; $p = $p->{"${_}::"} for split /::/, $pkg; return $p; } 1;
test.pl:
use strict; use warnings; open(my $fh, '<', $0); # Calls CORE::open use override open => sub { print "Hi\n"; }; open(my $fh2, '<', $0); # Prints Hi use override open => sub { print "Ho\n"; }; open(my $fh4, '<', $0); # Prints Ho use override open => undef; open(my $fh5, '<', $0); # Calls CORE::open
Output:
>perl test.pl Hi Ho
From there, I guess the idea is to make overrideautodie a lexical pragma. Shouldn't be too hard.
In reply to Re: Using delete package::{subroutine} to change resolution of subroutines
by ikegami
in thread Using delete package::{subroutine} to change resolution of subroutines
by pjf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |