Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Using can With Inheritance

This started out as a Q & A on using can, so that's how it'll start.

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.

A Scenario

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.

A Problem

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.

A Solution

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.

So, for example, here's a stripped-down version of my base class:

package A; # stuff taken out sub get_write_file_func { my $self = shift; return $self->can("_write_file"); }
The method "_write_file" is defined in my subclasses, so here's an example one of those:
package A::B; use A; use vars qw/@ISA/; @ISA = qw/A/; # stuff taken out sub _write_file { my($path, $val, $start) = @_; # ... }

In reply to Using can With Inheritance by btrott

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and not a whimper to be heard...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2018-06-20 00:32 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (116 votes). Check out past polls.