moose-class-person.txt

#=============================================================================
package Person;

use Moose;

has surname => (
    is => 'ro',
    isa => 'Str', # attribute need to be a string
    required => 1, # required in constructor
);

has forename => (
    is => 'ro',
    isa => 'Str', # attribute need to be a string
    required => 1, # required in constructor
);

has age => (
    is => 'rw',
    isa => 'Int', # attribute need to be a digit
);

sub name {
    my $self = shift;
    return join ", ", $self->surname, $self->forename;
}

#=============================================================================
package main;

my $fugitive = Person->new(
                   surname => "Doe",
                   forename => "John",
                   age => 18,
               );

print "!! Working example:\n";
print $fugitive->name, "\n";

print "\n" x 4; # seperate working with error

print "!! Missing parameters to person:\n";
my $invalid_person = Person->new(surname => "foo");