I have been dabbling a lot with Moose, a solid framework for object oriented programming in Perl. It is remarkably powerful and has transformed the way I look at Perl OO. It is also different enough from object oriented programming in other languages that I needed to create a section for it on coolnamehere.
Boilerplate
Perl OO has a reputation for boilerplate: large chunks of code that have little relation to the task at hand, but are necessary for the application to work at all. Fortunately, Moose cuts down significantly on the boilerplate. You can cut it down even more by taking advantage of the features available in recent versions of Perl, and that is exactly what I do in my code templates.
Since I am not being paid by the word, I ignored most of the Perldoc boilerplate that I use.
That’s it, really. You can move on if you’re not interested in my rambling explanations.
The Rambling Explanation
Let’s examine the boilerplate code.
This tells perl
that the program requires features that only become available in Perl 5.14. If I tried to load this library in an application using a different version of Perl, it simply would not work:
I also get Perl 5.12 and Perl 5.10 features, as long as they haven’t been made redundant by a change in the newest Perl.
This is one of those syntactical enhancements that I like in Perl 5.14. Here’s my package. It’s called “MyClass”. It has a $VERSION
of 1.0
. I suppose I could use MooseX::Declare, but I’m still getting the hang of core Moose functionality.
Now I’ve told Perl that the MyClass
package is actually a [Moose][] class. A lot of stuff is going on the background now, as all the Moose support structure is loaded and set up.
Okay, yeah. That part’s kind of obvious, isn’t it?
Now that I’m done with MyClass
, I want to get all the special Moose names out of the way.
A fully fleshed Moose object maintains a lot of flexibility. Unless I explicitly need that flexibility, I should make the class a little lighter.
That’s it. The package is done. It’s worth noticing what’s missing: the classic 1;
that has ended Perl modules for years. As far as I can tell, the new package
syntax makes it unnecessary. Perl 5.14 doesn’t complain about it missing.
Backlinks
Added to vault 2024-01-15. Updated on 2024-07-10