in reply to Determining what methods are available to a class
From perldoc UNIVERSAL...my $method = "build_a_kite"; if ($obj->can($method)) { print "We can!"; } else { print "We can't..."; }
$obj->can( METHOD ), CLASS->can( METHOD ), can( VAL, METHOD ) can checks if the object or class has a method called METHOD. If it does then a reference to the sub is returned. If it does not then undef is returned. This includes methods inherited or imported by $obj, CLASS, or VAL. can cannot know whether an object will be able to provide a method through AUTOLOAD, so a return value of undef does not necessarily mean the object will not be able to handle the method call. To get around this some module authors use a forward declaration (see perlsub) for methods they will handle via AUTOLOAD. For such 'dummy' subs, can will still return a code reference, which, when called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef will cause an error. can can be called as a class (static) method, an object method, or a function. When used as a function, if VAL is a blessed reference or package name which has a method called METHOD, can returns a reference to the subroutine. If VAL is not a blessed reference, or if it does not have a method METHOD, undef is returned.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Determining what methods are available to a class
by weierophinney (Pilgrim) on Aug 09, 2003 at 14:42 UTC |