Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

intercept nonexistent methods call

by Bor (Beadle)
on Aug 04, 2011 at 08:27 UTC ( #918465=perlquestion: print w/replies, xml ) Need Help??
Bor has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I try to intercept nonexistent methods call in some subclass.
Yes, I know about AUTOLOAD,
but (for methods) it try to call parent::method first, then UNIVERSAL::method and only then ::AUTOLOAD.
But I need call (something like) ::AUTOLOAD at first.
Because I want to know what methods subclass try to call from parent.
Give me some advice about it please.

Replies are listed 'Best First'.
Re: intercept nonexistent methods call
by Corion (Pope) on Aug 04, 2011 at 08:31 UTC

    If you don't want how inheritance dispatches method calls, maybe inheritance is the wrong way?

    Just create a separate class that inherits nothing and has an object of your "real" class. Give that class an AUTOLOAD method. In that AUTOLOAD method, log the calls and then either dispatch to the "real" class instance or whatever.

      Thank you for reply.

      Forgot to say: subclass - is a test mock module, so I have Module & Module::Mock.
      In tests I redefine like this
      local *Module::new = sub { Module::Mock->new(); };
      I want to see what methods called from this test module.
      And yes, you are right - maybe I not need inheritance from "real" class. Then I can control all methods calls.
        This is not working as I want :(
        If I comment "parent Module;" in Module::Mock.
        Then code
        local *Module::new = sub { Module::Mock->new(); };
        do not gives any effect and in other modules Module->new() calls.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://918465]
Approved by moritz
help
Chatterbox?
[Discipulus]: erix resize your elephant
[erix]: but elephant /are/ big
[Discipulus]: Lady_Aleena micro management of code, micro optimization is a pain, you'll brake working things
[Discipulus]: erix you are right but buy a bigger monitor for me then

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-05-27 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?