How not to use Moose

Maybe this is legitimate and I just don't know why, but I have seen the following in a couple of projects where I have taken over after the original programmer left:

use Moose;

has [qw/foo bar baz/], is => 'rw';

no Moose;

sub something {}
sub somethingelse {}

OK, I collapsed the attrib declaration into one line for brevity. I will say that from the code, I can tell you these are not Perl programmers, they are old time C programmers with CS degrees who convinced someone they knew what they were doing because they can write psudocode for finding the first 100 prime numbers and first 20 fibs. Somehow, they stumbled across Moose, but I think they quite got it - or maybe they did and I'm wrong? In the whole application this is the only way Moose is used and it seems to me there is about 20 ways to get attrib getter/setters lighter, if that is all you want. Maybe there should be a "Common Moose mistakes" in the documentation.

7 Comments

This type of coding is what I call, "Being clever." People do it to show off. But it makes maintaining the software difficult.

Don't be clever; be good.

Lighter in startup and memory maybe, but as far as i am aware, Moose accessors are faster than other implementations, especially without all the extra toys.

I can think of a couple of cases. For example:

The person was told that he has to use Moose or his code is not modern enough. So he uses whatever he was able to understand. This is a very unfortunate side effect of being enthusiastic about something.

The person thought to start learning Moose. He started to use it but in the end he did not need any of the features of Moose that make it so useful. Maybe he just did not have the time to further learn them so he reverted to what he already knew.

The person was told that he has to use Moose or his code is not modern enough.
This is a common "Modern" disease, which can even be caught from the core documentation these days.

There are various "Moose lite" here: Mouse, Moo, etc. depending on what subset of Moose is really required.

BTW. roles are awesome!

On the other hand, I don't see what's wrong with this (aside from the iffy class name and behaviour):


package Messiah;
use Mo;
has $_ => 'is' => 'rw' for qw/foo bar baz/;
sub boom { die "aargh"}
1;

I messed around with the single line assignment to get a collapsed declaration that actually compiles.

Leave a comment

About Jesse Shy

user-pic Perl pays for my travel addiction.