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


In reply to isa() on any scalar by mrborisguy

Title:
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, details, 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, summary, 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.