in reply to When I use "can"
Checking my current work application, the main reason I use can is when I'm about to call something that needs a callback supplied:
Note that in this situation, I know that the object must support the method.# template->capture(pattern, object, callback) # - replace each occurrence of <pattern> in the template # with the result of calling <callback> on <object> $template->capture('%id%', $self, $self->can('id'));
The other 3 uses each use only the boolean result:
in one delegating AUTOLOAD, purely to avoid the error message for an unknown method referring to the delegate;
in one virtual class to update a timestamp if the actual instance supports that timestamp;
in a debug method to shorten the output if the object being dumped supports an 'id' method.
(I also have three AUTOLOAD subs defined in the same application: the one described above; one for a 'security wrapper' class that pretends to be the wrapped object but blows up if you attempt to call any unpermitted methods; and one dummy object (which also provides its own can method) to allow some parts of the application to be used on an uninstalled copy.)
Hugo
|
|---|