I want my node class to be inheritable, but ultimately, the "parent" of any object should at least be of type Node.
Personally, I don't like using class membership to determine whether an object is "ok" or not. I mean, someone could use Node as a base class, but override all the methods to do anything they want anyway, so what's the point of looking for it? I'd rather go with what is sometimes called duck typing. That is, if it walks like a duck, and talks like a duck, it must be a duck. In Perl terms, this means I like to use can to check if an object has the methods I am going to call on it. For example:
sub use_object_somehow {
my ($obj, $foo, $bar) = @_;
carp "Object doesn't quack right" and return
unless $obj->can("foo") and $obj->can("bar");
$obj->foo($foo);
$obj->bar($bar);
}
This test is simple enough that I don't think it's too onerous to include in any subroutine that handles an incoming object, but it can be abstracted into a subroutine if so:
sub _check_obj {
my ($obj, @methods) = @_;
for (@methods) {
return unless $obj->can($_);
}
return 1;
}
Then it would be used like:
sub use_object_somehow {
my ($obj, $foo, $bar) = @_;
carp "Object doesn't quack right" and return
unless _check_obj($obj, qw(foo bar));
$obj->foo($foo);
$obj->bar($bar);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.