oocan - Q and A on the can function

Published at: 2000.08.18 23:38:26

NAME

oocan - Q and A on the can function

SYNOPSIS

How can I determine whether a particular object has a particular method?

DESCRIPTION

Use the [can] method on your object. From [perlobj]:

    can(METHOD)

    can checks to see if its object 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 is relatively simple and straightforward. I found a rather interesting use for it the other day, though. Here was the scenario:

I had created (basically) an abstract base class for holding a tree-like data structure. The base class contained all of the methods necessary to interact with the data structure--to get data out, to put data in, etc.

But the base class didn't have any notion of how to read the data in from an external source; that was to be provided by the subclasses.

So I also defined two subclasses: one for processing XML data and one for processing a different format.

The base class also had a recurse method--supplied with a callback method, it would recurse into the data structure and call the callback for each node found in the tree. I had defined some simple callbacks: one to pretty-print the data to the screen; one to dump the data in HTML format using unordered lists; and one to write the file to disk in either XML or this other format. These callbacks were ``private'' functions, though, so I also provided accessor methods to return sub references to the callbacks.

The problem, though, was that these accessor methods were defined in my base class, and I wanted to allow for my subclasses to override the callback methods. However, I (obviously) wouldn't know whether or not they overrode the methods until run-time, so I couldn't hard-code the subclass name into my accessor functions.

So I used can. When I defined a new object, it was blessed into one of my subclasses; so, in my accessor methods, I realized that I could call can with the callback method name as the argument, and I'd get back the ``correct'' version of the callback method.



© 2000 Benjamin Trott, ben@rhumba.pair.com