|Just another Perl shrine|
isa() on any scalarby mrborisguy (Hermit)
|on Jun 10, 2005 at 17:36 UTC||Need Help??|
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.
However, I've also got a test that I'm building along with this node class, in which I have these lines:
(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):
I'm wondering if there is a better way that doesn't look quite as ugly. Anybody have any advice for me?