Since it's Sunday, I had a couple extra hours to spare in the evening, so I decided to play around some more with Moose.
More specifically I've seen MooseX::Declare mentioned on several occasions so why not give it a go myself.
This module provides a fancier declarative syntax you can use to define objects much more in the tradition of other so-called 'real' OOP languages. It also automatically includes MooseX::Method::Signatures, which means I can also use method declarations with type constraints to my heart's delight.
Also within the method declarations you get the good old $self as a freebie, meaning you don't always have to remember to use the my $self = shift statement. This makes the whole method declaration syntax cleaner and more elegant.
So having said all that here follows my simple example in which I'm able to try out some of the interesting deals on offer.
use MooseX::Declare;
use feature qw(say);
class Person {
has 'age' => ( isa => 'Num', is => 'rw', default => 0 );
has 'max_age' => ( isa => 'Num', is => 'rw', default => 75 );
method get_older (Int $years_to_add = 1 where { $_ > 0 }) {
$self->age( $self->age + $years_to_add ) if $self->is_alive;
}
method is_alive {
return $self->age < $self->max_age;
}
method speak {
say $self->is_alive ? "I'm alive!" : "I've kicked the bucket!";
}
}
So let's see how good I've coded this, create a Moosified Person object and see if it all works.
my $x = Person->new( max_age => 35 );
while ($x->is_alive) {
$x->get_older( 5 );
$x->speak;
}
When I ran the program, it results in the following not so amazing output:
I'm alive!
I'm alive!
I'm alive!
I'm alive!
I'm alive!
I'm alive!
I've kicked the bucket!
I guess it works good, pretty darn good, will have to investigate this interesting functionality in more detail another day.



Recent Comments
- nikolay.mishin
- Kiffin
- Kiffin
- ice cream maker
- Anonymous