Search⌘ K

Class and Methods

Explore how to create classes and methods in Perl using Moose. Understand the difference between instance and class methods, how to use constructors, and how methods interact with objects for maintainable and idiomatic Perl code.

Classes

A Moose object is a concrete instance of a class, a template describing data and behavior specific to the object. A class generally belongs to a package, which provides its name:

Perl
package Cat {
use Moose;
}

This Cat class appears to do nothing, but that’s all Moose needs to make a class. We can create objects (or instances) of the Cat class with this syntax:

Perl
package Cat {
use Moose;
}
my $brad = Cat->new;
my $jack = Cat->new;

In the same way that this arrow operator dereferences a ...