in reply to Using delete package::{subroutine} to change resolution of subroutines
Two problems.
First, as the warning indicates, the main::open in
*{main::open} = sub { print "Ho\n"; };
is interpreted as a call to open. (Upd: Not sure how it's being interpreted anymore, but it's definitely not working as expected. ) You want
my $glob_ref = do { no strict 'refs'; \*{'main::open'} }; *$glob_ref = sub { print "Ho\n"; };
Second, you delude yourself by putting the use subs at the bottom.
BEGIN { # Deleting and remaking the sub fails! ;( delete $main::{open}; *{main::open} = sub { print "Ho\n"; }; use subs 'open'; }
means
BEGIN { # Deleting and remaking the sub fails! ;( delete $main::{open}; *{main::open} = sub { print "Ho\n"; }; BEGIN { require subs; import subs 'open'; } }
which is compiled and executed as follows:
"import subs 'open';" should not be executed before "delete $main::{open};".
Fixed:
use strict; use warnings; use subs qw( ); open(my $fh, '<', $0); # Calls CORE::open BEGIN { my $glob = do { no strict 'refs'; \*{'main::open'} }; *$glob = sub { print "Hi\n"; }; import subs 'open'; } open(my $fh2, '<', $0); # Prints Hi BEGIN { delete $main::{open}; my $glob = do { no strict 'refs'; \*{'main::open'} }; *$glob = sub { print "Ho\n"; }; import subs 'open'; } open(my $fh4, '<', $0); # Prints Ho BEGIN { delete $main::{open}; } open(my $fh5, '<', $0); # Calls CORE::open
|
|---|