Moose introduces declarative syntax for objects in Perl similar to Ruby. Moose is just another package technically and it can be used like any other package by including it with use Moose
.
Following are some fundamentals of OO in Perl:
Every package is a class:
package packagename
means that the code following in the rest of the scope is defining a class calledpackagename
.Every subroutine is a method: Every subroutine in a class becomes an object method. Can be avoided by using
namespace::autoclean
.An object is only a blessed reference: Any reference that gets blessed is an object that can use the methods from the blessing class.
We are familiar with the following in the context of a class:
Properties
Methods that can manipulate these properties
Ways of instantiating itself as objects
Let us explore how Perl addresses this. Properties are achieved by creating a reference and blessing it. Please note that a blessed reference is an object. There's nothing more to it...