Moose Mashing

Does Moose eliminate the sin of False Impatience* by making it so damn easy to just add a dash of OO to your script in order to avoid messing around with data structures? That's how I've started, so there's bound to be a few contusions as I find the corners of my own ignorance, digging out the notes from Dave Cross' most excellent Perl School on the subject. It's not pretty, but it gets the job done.

This is a story about how hacking away at a script taught me some things that I should have known.

The second time I used Moose in anger was to manage lists of group members, a 2 attribute, 2 method class, at the top of my original script after I'd loaded in my local utility modules with helper functions (exported by Exporter).


use Keele::Utilities::Database;
use Keele::Utilities::LDAP;

package GoogleGroups;
use Moose;

My script dies with the error that it can't find the function exported from the Utilities module. So I move the class declaration to the end of the script and the error is that it can't find the object accessor. Hmmm. ... Eventually solved by putting the class declaration at the top and useing the utility modules after the __PACKAGE__->meta->make_immutable; call. Y'know, it's staring me in the face right now that the package line should have been the first thing in the script, but hey.

The next fun bit was remembering to add in a default empty hashref to hold the members



has 'members' => (
is => 'rw',
isa => 'HashRef',
default => {},
);


and then finding that it needs to be wrapped in an anonymous sub


default => sub{ {} },

In the end, it works, it took as long as it would have had I taken the time to get the data structures correct with plain old hashrefs and I'm more comfortable with Moose. If my crib sheet begins to approach anything close to useful, I'll post it.

Postscript: A lot of things become obvious when you start to tell someone else about them. Perhaps if I'd blogged first, I could have saved myself an hour faffing around.

* Camel book, 2nd ed. Packages, Modules and Object Classes, p277

2 Comments

Regarding your postscript: you have discovered the joys of Rubber Duck Debugging. Also you may want to try Moo (vs Moose) for day-to-day use as it is faster.

Leave a comment

About Enkidu

user-pic I am a Freelance Scientist** and Perl is my Igor.