Everywhere you look these days in Perl literature, chances are that you'll come across references to Moose
, how great and flexible it is and so forth.
Since I had such a lousy game of golf this afternoon and lost my first matchplay round six down, I needed a way to inspire myself. So what could be better then reading the Moose::Manual
this evening?
Boy, was I in for a frustrating surprise.
I hate to admit it, but all this Moose
stuff is not easy for me (grumble). Rather than getting all excited and inspired like I hoped, I'm sorry to admit that it's making me feel rather sad that I cannot figure this out.
Am I supposed to be able to understand this Moose
stuff? After so many years of getting pleasure in tackling all kinds of complex programming issues, have I simply reached my limit? Time to give up trying to be a Perl guru, jump ship, and be forced to become a golf hacker the rest of my life? What do other fine Perl souls out there think about it?
Here's an example that better illustrates my frustrations. In the Moose::Manual::Concepts
one goes on and on about classes
, attributes
, methods
and roles
. Not bad. A role
has zero or more of this, a role
has zero or more of that, a role
has zero or more of this and that. Getting more difficult.
Later on one learns that a role
is somewhat like mixins
or interfaces in other OO languages. Getting more difficult. What the heck is a mixin
(and how do you pronounce it)? The good old Wikipedia saves the day with a nice, clear definition:
"In object-oriented programming languages, a mixin
is a class that provides a certain functionality to be inherited by a subclass, while not meant for instantiation (the generating of objects of that class). Inheriting from a mixin
is not a form of specialization but is rather a means of collecting functionality. A class may inherit most or all of its functionality from one or more mixins
through multiple inheritance."
Huh? Nearing apogee and getting impossibly difficult.
Whatever happens, I have promised myself to hang in there. Read and reread as much as I can. Stay smart. Get smarter. Figure it all out or else.
What with so many Perl gurus out there getting all fired up about Moose
, mixin
and stuff
, why I want to join the fun as well!
Tomorrow it's all about Moose::Manual::Classes
and then next the world.
Wish me luck.
Theory can be difficult, especially if the terms used are unfamiliar. So learn by example! Try reading Moose::Cookbook. There are a bunch of recipes which go through roles.
Roles are one of the hardest things to grasp really, there is no clear analog in traditional Object Orientation. This means of course that they are one of the most powerful and interesting aspects of Moose. Comparing to mixin's is obviously an attempt to give an analogy to something some people may have known (Ruby uses them extensively I'm given to understand), another analogy people have given is Java Interfaces that actually provide a default implementation.
Ultimately though the power to Moose isn't any single feature but is the mixture of powerful tools that provide the effects demonstrated in Moose::Manual::Unsweetened. Some of those tools are more complex (Roles) and some are more simple (attributes and generated methods) to understand. Moose like any good toolbox provides you the ability to work with just a hammer before having to learn the jigsaw.
Yeah, I had a good look at Moose::Manual::Unsweetened in the train this morning on my way to work and was impressed by all that cleverness. Actually, by understanding better what the heck Moose does to plain old Perl 5, I'm beginning to grasp the Moose essentials more firmly. Still confused with Roles though. By the way they aren't quite Java interfaces, and such analogies should better be avoided I believe.
Roles are code reuse without inheritance (and implicit relationship is-a between classes). It was thanks to Roles description that I had understood what mixins are ;-)
So rather than trying to study mixims in order to understand roles better, it's the other way around. Kind of a catch-22.