Chapter 5. Object oriented programming

5.2. Classes and objects

Simple class definitions

Class definitions (aka "packages" or "modules") go in files with the suffix .pm (Perl Module):

$ mkdir lib
$ vi lib/

The code:

package Godzilla;
use Moo;

sub breath_fire { ... }
sub move_to     { ... }

sub attack {
    my ($self, $where, $when) = @_;


Creating objects


use Godzilla;
my $godzilla = Godzilla->new(@args);
$godzilla->attack("Tokyo", "later");

Run the code:

perl -Ilib

Object destruction

Once an object goes out of scope it is destroyed. If you need to do any cleanup in a Moo object put that logic into a method called DEMOLISH().

    my ($self) = @_;