Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: I'm in trouble with Perl's OO programming

by revdiablo (Prior)
on Jun 09, 2005 at 17:30 UTC ( [id://465216]=note: print w/replies, xml ) Need Help??


in reply to I'm in trouble with Perl's OO programming

The solution which consist in calling the "can" method works

It sounds like you have found a working solution, but in case you still wonder why your original code didn't work (and for the sake of others who don't know), I'll reply to your code as originally posted.

The main problem I see is that you're trying to take a reference to a method. The thing is, a method is just a plain subroutine that has been called against an object. It doesn't become a method until a certain action is taken. You can't directly take a reference to that action; you can only take a reference to the subroutine.

When you take a reference to a subroutine, you are taking a reference to that exact subroutine. It is unambiguous. It does not travel the inheritance tree looking for parent packages with subroutines named the same thing. That doesn't even make sense all the time, as subroutine references can be anonymous, in which case they neither belong to a package nor have a name.

So what you need to do is encapsulate the action in a reference. The way to encapsulate action is with subroutines, so you have to create another subroutine that calls the target as a method. This may sound like a hassle, but Perl makes it pretty simple:

$self->addTab(sub { $self->WatchLog });

Now, when you want to execute that reference, you don't need to do any additional work to call it as a method. It already encapsulates its own method-calling behavior, so you can simply execute the coderef:

foreach my $code (@{$self->{CODETAB}}){ $code->(); }

This also gives you the flexibility to create code tabs that aren't methods. I'm not sure if that's a flexibility you really want, but it's definitely something I would consider.

As for the can solution, I imagine you are doing something along the lines of:

$self->addTab($self->can("WatchLog"));

This avoids your problem by traversing the inheritance tree to find the subroutine reference. It still takes a direct reference to the subroutine, but this time it's the correct subroutine, and you can fudge the method-calling semantics as you did before.

Update: typo, s/corrent/correct/

Replies are listed 'Best First'.
Re^2: I'm in trouble with Perl's OO programming
by cybergeek (Novice) on Jun 10, 2005 at 11:24 UTC
    Thank you so much for this explanation. It is now clearer to me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://465216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found