If you asked, "How can I determine whether a particular object has a particular method?", an answer would be:
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.
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.
So, for example, here's a stripped-down version of my base class:
The method "_write_file" is defined in my subclasses, so here's an example one of those:package A; # stuff taken out sub get_write_file_func { my $self = shift; return $self->can("_write_file"); }
package A::B; use A; use vars qw/@ISA/; @ISA = qw/A/; # stuff taken out sub _write_file { my($path, $val, $start) = @_; # ... }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using can With Inheritance
by JadeNB (Chaplain) on Jul 22, 2008 at 23:19 UTC |