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 use
ing 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
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.
I get the concept, but seem to lack the discipline to see the duck as an external entity. He just nods his little rubber head and never tells me how much of an idiot I've been. There is just something about writing for an audience that gives me the discipline to take the time to explain the problem (which indicates I should be spending more time in the documentation wiki) That said, I've taken Catbert off the shelf and sat him next to the monitor.
From way back, I remember that I could sometimes pull the solution to difficult problems out of the air by simply looking up and to the left. NLP will tell you that's the direction a right-handed person will look while visually constructing an image. Sure as hell staring at the screen won't help.
Dagnabbit! I've just rubber ducked again. I remember that I can't tell my right from my left having cheated at that task in kindergarten (because I was very good at symmetry) Solutions are up and to the right ... I think.