MooseX declare and method signatures

| Perl | 3 Comments

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.

3 Comments

MooseX::Method::Signatures is syntactically very nice, but there are a few problems. One is the error messages... which can be hard to decipher. More importantly, start-up time takes a massive hit. It loads a huge amount of modules (including PPI). That's also going to be a large memory footprint, which can be an issue (e.g. under mod_perl).

Also, the way it works can cause conflicts with other modules. I haven't quite tracked it down, but I've seen very odd compile-time errors produced when using it with Module::Pluggable.

In short, I think it's a cool idea, and show what's possible. But I wouldn't recommend it for production code. There's just too much dark magic going on, which is just not worth the pain in the long run. Just like you wouldn't use source filters in production.

thanks for such cool example for Moose

Random entries

Here are some random entries that you might be interested in:

Recent Assets

  • tegen-par-2024-2nd-place.jpeg
  • stanford-reunion.png
  • kiff.png
  • hoid.png
  • Dad-in-front-of-log-cabin.png
  • mistborn-trilogy.png
  • 2024-03-Heren1-27h.png
  • three-body-problem.png
  • 10CC.png
  • minds-and-machines.jpeg
  • puglia.png
  • 2023-09-24-jong-tegen-oud-1.jpg

Recent Comments

Golf Handicap

Information

This personal weblog was started way back on July 21, 2001 which means that it is 7-21-2001 old.

So far this blog contains no less than 2518 entries and as many as 1877 comments.

Important events

Graduated from Stanford 6-5-1979 ago.

Kiffin Rockwell was shot down and killed 9-23-1916 ago.

Believe it or not but I am 10-11-1957 young.

First met Thea in Balestrand, Norway 6-14-1980 ago.

Began well-balanced and healthy life style 1-8-2013 ago.

My father passed away 10-20-2000 ago.

My mother passed away 3-27-2018 ago.

Started Gishtech 04-25-2016 ago.