Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^6: Point me in the right direction with OO inheritance and static variables

by Amblikai (Scribe)
on Sep 10, 2014 at 09:46 UTC ( [id://1100108]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Point me in the right direction with OO inheritance and static variables
in thread Point me in the right direction with OO inheritance and static variables

Thanks very much for all this, it's great info

My program is coming along really well now. I found the hardest part isn't so much the coding but the architecture of the classes and when to inherit or just modify the existing class. As a learning experience it's been really good.

I have one more "problem" though. I'd like to be able to make sure, that when the object is first created, the data it is created with is in the correct format.

I think this is similar to Moose's 'isa' keyword after a bit of googling. But again, i'd like to do it in pure perl

I've no problems writing the actual code, but my problem is where to put it?

Any help or pointers? Thanks again!

  • Comment on Re^6: Point me in the right direction with OO inheritance and static variables

Replies are listed 'Best First'.
Re^7: Point me in the right direction with OO inheritance and static variables
by tobyink (Canon) on Sep 10, 2014 at 20:37 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1100108]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-18 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found