Determining if a method is defined for an object 
Author Message
 Determining if a method is defined for an object

Suppose you have (a reference to) and object in $obj and the name of a
method in $method.

I'm interested in how you could determine whether the method is
defined for the object.  I'm especially interested if it can be done
without symbolic references.

I have seen one solution (which uses symbolic references and
catenation).  But it doesn't feel that satisfying.

The fact that $obj->$method is an invocation and not a dereference is
so sad (in this context), because it means that you can't look and see
if the 'reference' is defined....

Is there some other way that I could have (a representation of) the
method (other than a name in a string) that would result in a
different problem that has a more satifying solution?  ( :-) )

Martin.



Sun, 12 Mar 2000 03:00:00 GMT  
 Determining if a method is defined for an object



Quote:

>Suppose you have (a reference to) and object in $obj and the name of a
>method in $method.

>I'm interested in how you could determine whether the method is
>defined for the object.  I'm especially interested if it can be done
>without symbolic references.

print "yes it can\n" if $obj->can($method);

The "can" method is inherited from the UNIVSERSAL class. I can't
remember how recently UNIVERSAL::can was introduced though, so if
you're not running 5.004 you may need to upgrade.

--Malcolm

--

Oxford University Computing Services
"I permitted that as a demonstration of futility" --Grey Roger



Mon, 13 Mar 2000 03:00:00 GMT  
 Determining if a method is defined for an object

Quote:


> > Suppose you have (a reference to) and object in $obj and the name of a
> > method in $method.

> > I'm interested in how you could determine whether the method is
> > defined for the object.  I'm especially interested if it can be done
> > without symbolic references.

> AUTOLOAD is not your friend for this one.  x->y() does not imply that
> "y" exists anywhere.  Even if "y" exists, it could be in a different


        <snip>

Quote:
> Sorry, but I don't see any simple answers to "is y a valid method for
> x".

Via email Guy Decoux reminded me that "can" does handle the ISA
inheritance, but still doesn't work for AUTOLOAD.

- doug



Mon, 13 Mar 2000 03:00:00 GMT  
 Determining if a method is defined for an object

Quote:

> Suppose you have (a reference to) and object in $obj and the name of a
> method in $method.

> I'm interested in how you could determine whether the method is
> defined for the object.  I'm especially interested if it can be done
> without symbolic references.

AUTOLOAD is not your friend for this one.  x->y() does not imply that
"y" exists anywhere.  Even if "y" exists, it could be in a different

Quote:
> I have seen one solution (which uses symbolic references and
> catenation).  But it doesn't feel that satisfying.

> The fact that $obj->$method is an invocation and not a dereference is
> so sad (in this context), because it means that you can't look and see
> if the 'reference' is defined.....  

You mean you have to use eval() to trap the error of the method not
existing?  I agree that this is ugly.

Quote:
> Is there some other way that I could have (a representation of) the
> method (other than a name in a string) that would result in a
> different problem that has a more satifying solution?  ( :-) )

Could do something like

        $method = \&y;

You couldn't get to the call unless you were sure that y() existed.  Of
course, AUTONET supplied functions can't be done this way.  You would
have to manually go through the superclasses of x to look for y too.

Sorry, but I don't see any simple answers to "is y a valid method for
x".

- doug



Mon, 13 Mar 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. determining existence of object method

2. determining existence of object method

3. determining existence of object method

4. Win32::OLE object as parameter to method to another OLE object

5. How to determine AM or PM time

6. Determining if a filehandle is defined

7. Which method am I on?

8. Quick way to determine if a method is implemented in OO module

9. DB_File::BTREEINFO does not define the method CLEAR ?

10. AnyDBM_File doesn't define EXISTS method?

11. AnyDBM_File doesn't define an EXISTS method

12. Error: AnyDBM_File doesn't define a TIEHASH method

 

 
Powered by phpBB® Forum Software