Making Perl class definitions more conventional and shorter
Archive - Originally posted on "The Horse's Mouth" - 2010-12-20 21:16:18 - Graham EllisPerl 5's object oriented capabilities are excellent - but often a little longwinded, and quite easy to get wrong. Moose sells itself as "postmodern object system for Perl 5 that takes the tedium out of writing object-oriented Perl"; it's available for download from the CPAN under same license as Perl itself, and provides a good solution for certain groups of people. There does, however, seem a lack of "start here" examples ... so I've added a few to the web site.
Simplest Moose class - setting up an automatic constructor with two properties - [source].
Adding default values - [source].
Using Moose to check data types - [source].
Calling automatically generated functions, and with read only and read-write settings - [source] This is the first practical example as it sets up the access to properties via methods rather than direct variable access
Inheritance (using the extends keyword) and roles (i.e. interfaces / the requires keyword) in Moose - [source].
Getters, setters and defaults in Moose - [source].
Additional functionality - adding a second method with the after keyword - [source].
Would I use Moose on a project? If there were many classes to be written, and I was writing from scratch I might well give it some careful thought. It is just a wrapper around a blessed hash, but sometimes the wrapping can make all of the difference!