mrborisguy has asked for the wisdom of the Perl Monks concerning the following question:
This week is Learn-How-To-Program-OO-Correctly week for me, so here's another question about it!
I want to make a class called "Node", like the nodes you find in trees or lists (more for playing than actually for anything useful). This Node class has a "parent" attribute. I want my node class to be inheritable, but ultimately, the "parent" of any object should at least be of type Node.
if( $value->isa( "Node" ) ) { # It's okay to put $value into the parent attribute }
However, I've also got a test that I'm building along with this node class, in which I have these lines:
$obj->parent( 0 ); is( $obj->parent, undef );
(undef because I haven't changed the parent, and since 0 isn't a Node, the value doesn't get changed).
Okay, so I guess I better get to the question soon. I want to check if $value-isa( "Node" ), but the problem is that $value can be anything, and if it's a SCALAR, then isa doesn't work! I tried this code, which works (I think):
if( ref( $value ) && ref( $value ) ne "SCALAR" && ref( $value ) ne "AR +RAY" && ref( $value ) ne "HASH" && $value->isa( "Node" ) ) {
I'm wondering if there is a better way that doesn't look quite as ugly. Anybody have any advice for me?
-Bryan