I have a value that can be specified as a single number or as a range, and I use e.g. [3,5] for the range and a plain scalar if only one value is needed. So, you can call foo(5) or foo([5,6])

So, I wrote

if (ref $value eq "ARRAY") {
to test my parameter. But, I got to thinking. What if it's not really an array, but a class that looks like an array, or a tie? Should I use isa instead?

If I eventually turn the range into a class by blessing the array reference and adding some members to, say, get the difference, then the isa would be handy. I think a tie would still test as an array.

But, in a related thought, I have some code that wants a number. Now, what if it's not really a number, but a 'bignum' class that overloads all the operators so it (mostly) can be used like a number? In this case, ideally I wouldn't care, because if it quacks like a duck it must be a duck. I want to test for the quack, not the duck. For $x+2 I don't care that $x is a class object not a scalar, but what about as an argument to pack? I suspect that would not work properly.

So, how much testing should one do to see if arguments are what are expected, and how does "overloading" functions to understand different data types complicate matters?

Any thoughts?

—John


In reply to if (ref $value eq "ARRAY") { by John M. Dlugosz

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.