My answer applies only to OO stuff ( see above regarding when to use prototypes ).

For objects, the ->isa method is commonly used to check "Is an object of this class".

Of course, if you are taking arguments from outside the code you control, there's no way to know if ->isa will work, since isa'ing something that isn't an object will die.

BUT, calling UNIVERSAL::isa as a function directly CAN handle non-reference arguments, and correctly returns false. ( i.e. UNIVERSAL::isa( undef, 'Something' ) works, and correctly returns false )

I'm also a big fan of doing type checking ( where it doesn't get too large of course ) into the variable decleration. I'm also a fan of using return undef; to symbolise an error. Even in the cases where you have true/false responses, you can use true/false/undef ( 1/''/undef )

SO, for most of my OO stuff, and there's a lot of it, I use something like the following.

package Foo; use strict; sub bar { my $File = UNIVERSAL::isa( $_[0], 'IO::File' ) ? shift : return un +def; # Do something to file } 1;
Of course, all those UNIVERSAL::isa's get virtually RSI like to type, so for convenience, I use the following.
package Foo; use strict; use UNIVERSAL 'isa'; sub Foo { my $File = isa( $_[0], 'IO::File' ) ? shift : return undef; # Do something to the file } 1;
I find this highly useful, especially for reasonably large quantities of arguments. Because you don't shift first and check later, you never forget to check.

In reply to Re: validating function input by adamk
in thread validating function input by bobdeath

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.