To simplify (read: lie about what's happening) the explanation: you need to get at the overridden method without going to the typeglob. You can achieve that with the even uglier:
no warnings 'redefine'; my $real_new; BEGIN { $real_new = Foo->can( 'new' ) } *Foo::new = sub { if (caller() eq 'Bar') { print "Returning real Foo.\n"; return Foo->$real_new(); } print "Returning fake Foo.\n"; return FakeFoo->new; };
... to force the lookup to happen before the glob replacement, but don't do that. Remove that caller magic from the constructor and make it an external parameter. You're far better off that way.
In reply to Re^4: Testing -- making a new constructor behave differently for one package
by chromatic
in thread Testing -- making a new constructor behave differently for one package
by tj_thompson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |