Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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/


In reply to Re: I'm in trouble with Perl's OO programming by revdiablo
in thread I'm in trouble with Perl's OO programming by cybergeek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found