As Eric noted, you can usually use UNIVERSAL::can
It can be used either on an object as in your example ($obj->can($methodName)
) or statically, on a class: (CLASS->can($methodName)
)
Please note that there are possible false negatives associated with using UNIVERSAL::can
on objects/classes which have AUTOLOAD-ed methods - see the perldoc for details. So before using can()
on an object/class, please be careful to verify that the class in question either does not use AUTOLOAD, or overrides can()
to compensate, or uses forward declaration to compensate as described in can()
's perldoc - hat tip to brian d foy)
Also, please be careful to either ONLY call can()
on actual objects, or encapsulate it in eval. It will die if called on a non-object (e.g. undef, scalar etc...)