How to tell if a method exists

B

Brian Miller

Hello All,

I am attempting to find a way to see if a method exists within a
specific package. I cannot use the PACKAGE->can(method) because
it follows the class hierarchy and it is possible that the method
exists somewhere else. What I was hopping to do is to have a base class
that scans the class hierarchy of an object and executes (in a specific
order) a method (let's say post_init). This would eliminate the need
for each method to run through it's package @ISA array and execute the
parents version of the method. If anyone has a suggestion I would
greatly appreciate it.

Thanks
Brian Miller
(e-mail address removed)
 
M

Malcolm Dew-Jones

Brian Miller ([email protected]) wrote:
: Hello All,

: I am attempting to find a way to see if a method exists within a
: specific package. I cannot use the PACKAGE->can(method) because
: it follows the class hierarchy and it is possible that the method
: exists somewhere else. What I was hopping to do is to have a base class
: that scans the class hierarchy of an object and executes (in a specific
: order) a method (let's say post_init). This would eliminate the need
: for each method to run through it's package @ISA array and execute the
: parents version of the method. If anyone has a suggestion I would
: greatly appreciate it.

: Thanks
: Brian Miller
: (e-mail address removed)

Localize the appropriate @ISA and then do the lookup.

The syntax of the following should be made prettier, but it illustrates
the point. The output from running the following shows that when
"can_no_isa" is called with the correct package (TWO) then the object
can't. The other package names are just to confirm that the routine is
doing the right thing.

I guess you could also check if the routine was defined in the package
hash but that didn't work in my quick example (!) so I gave up and just
tried the following instead.


#!perl

use strict;

sub can_no_isa
{
my ($pack,$meth,$obj) = @_;
no strict;
local @{"${pack}::ISA"} = ();
return $obj->can($meth);
}

package ONE;

sub try {'ta-dah!'}

package TWO;
our @ISA = qw(ONE);

package main;

my $obj = bless \my $x , 'TWO';

print 'obj can try = ' , $obj->can('try') , "\n";

print 'obj can_no_is try in THR = ' , can_no_isa('THR','try',$obj) , "\n";
print 'obj can_no_is try in TWO = ' , can_no_isa('TWO','try',$obj) , "\n";
print 'obj can_no_is try in ONE = ' , can_no_isa('ONE','try',$obj) , "\n";
 
J

Jeffrey Schwab

Brian said:
Hello All,

I am attempting to find a way to see if a method exists within a
specific package. I cannot use the PACKAGE->can(method) because
it follows the class hierarchy and it is possible that the method
exists somewhere else. What I was hopping to do is to have a base class

Let's call this base class BaseClass.
that scans the class hierarchy of an object

Let's call this object Object, and its class ObjectClass. Is
ObjectClass a descendant of BaseClass? Also, I thought the problem with
PACKAGE->can(method) was that method might not be in the class
hierarchy, so why do you want to scan that hierarchy?
and executes (in a specific
order) a method (let's say post_init).

Do you mean the post_init method of each class in ObjectClass's hierarchy?
This would eliminate the need
for each method to run through it's package @ISA array and execute the
parents version of the method.

.....at the expense of having a method in BaseClass run through the same
set of classes. Or have I misunderstood?
 
B

Brian McCauley

Brian said:
I am attempting to find a way to see if a method exists within a
specific package. I cannot use the PACKAGE->can(method) because
it follows the class hierarchy and it is possible that the method
exists somewhere else.

exists &{"${package}::$method"};

Note: this ignores subroutine attributes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top