Perl 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!
(written 2010-12-20, updated 2010-12-24)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P310 - Perl - Moose [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
P218 - Perl - More Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[246] When to bless a Perl variable - (2005-03-15)
[531] Packages in packages in Perl - (2005-12-16)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[592] NOT Gone phishing - (2006-02-05)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[930] -> , >= and => in Perl - (2006-11-18)
[1217] What are factory and singleton classes? - (2007-06-04)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1664] Example of OO in Perl - (2008-06-03)
[1665] Factory method example - Perl - (2008-06-04)
[1819] Calling base class constructors - (2008-10-03)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2811] Igloos melt in the summer, but houses do not - (2010-06-15)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
[3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
[3941] Building an object based on another object in Perl - (2012-12-03)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
Some other Articles
The week before ChristmasLooking ahead and behind in Regular Expressions - double matchingPerl - database access - DBD, DBI and DBIx modulesMaking Perl class definitions more conventional and shorterContrast in picturesThe Christmas Season has arrivedSetting your user_agent in PHP - telling back servers who you areHow many toilet rolls - hotel inventory and useagewxPython geometry - BoxSizer example