Try downloading and installing my Type-Tiny distribution from the CPAN, which offers various type checks and is strongly modelled on the Moose type constraint system.

You can do type checks like:

use Types::Standard qw( Int ); if ( not Int->check($var) ) { die "It's supposed to be an integer!\n"; }

The check method shown above will return a boolean. There's also an assert_return method. This will spew out its own error message if the value doesn't pass its type check, so you don't need to do the if not ... die stuff. (And if the value does pass, it simply returns it.)

use Types::Standard qw( Int ); Int->assert_valid($var);

Here's how you could do the type checks for a function:

use Types::Standard qw( Object Num ); sub transfer_money { my $from_account = Object->assert_return( shift ); my $to_account = Object->assert_return( shift ); my $amount = Num->assert_return( shift ); ...; }

One of the other modules that is bundled along with it is called Type::Params, and simplifies type checks for functions even more, and is pretty damn fast.

use feature qw( state ); # enable the `state` feature from Perl 5.10+ use Types::Standard qw( Object Num ); use Type::Params qw( compile ); sub transfer_money { state $check = compile( Object, Object, Num ); my ($from_account, $to_account, $amount) = $check->(@_); ...; }

Even if you decide against using it in your final code, it might help you learn how to do various type checks. For example, if you want to know how to test if $val is an integer or not, you can do:

perl -MTypes::Standard=-all -le'print Int->inline_check(q($val))'

This will print out a string of Perl code that shows you how Types::Standard does the check.


In reply to Re^7: Point me in the right direction with OO inheritance and static variables by tobyink
in thread Point me in the right direction with OO inheritance and static variables by Amblikai

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.