in reply to Re^3: Testing -- making a new constructor behave differently for one package
in thread Testing -- making a new constructor behave differently for one package
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Testing -- making a new constructor behave differently for one package
by Anonymous Monk on Jan 28, 2012 at 03:42 UTC |