++ed by:

4 PAUSE users
7 non-PAUSE users.

Author image Stevan Little


UNIVERSAL::Object - A useful base class


version 0.01


    package Person;
    use strict;
    use warnings;

    our @ISA = ('UNIVERSAL::Object');
    our %HAS = (
        name   => sub { die 'name is required' }, # required in constructor
        age    => sub { 0 },                      # w/ default value
        gender => sub {},                         # no default value

    sub name   { $_[0]->{name}   }
    sub age    { $_[0]->{age}    }
    sub gender { $_[0]->{gender} }

    package Employee;
    use strict;
    use warnings;

    our @ISA = ('Person');
    our %HAS = (
        %Person::HAS, # inheritance :)
        job_title => sub { die 'job_title is required' },
        manager   => sub {},

    sub job_title { $_[0]->{job_title} }
    sub manager   { $_[0]->{manager}   }

    # ...

    my $ceo = Employee->new(
        name  => 'Alice',
        title => 'CEO',

    my $manager = Employee->new(
        name    => 'Bob',
        title   => 'Middle Manager',
        manager => $ceo,

    my $pawn = Employee->new(
        name    => 'Joe',
        title   => 'Line Worker',
        manager => $manager,


This is a simple base class that provides a protocol for object construction and destruction. It aims to be as simple as possible while still being complete.


One of the key contributions of this module is to provide a mechanism for declaring the slots that a given class is expected to have. These are used in the object contruction process to ensure that all slots are created and initialized.


This is a public (our) package variable that contains an entry for each slot expected in the instance. The key is the slot's name, while the value is a CODE reference which, when called, will produce a default value for the slot.

NOTE: No inheritance of slot definitions is done between classes, this is left as an exercise to the author, however it is easily accomplished with the pattern shown above in the SYNOPSIS.

SLOTS ($class)

This is an accessor method for the %HAS variable in the $class package.

NOTE: If you choose to store the slot definitions elsewhere (not in %HAS) or store them in a different form (not key => CODE hash entries), it is possible to then override this method to return the expected values in the expected form.


Once we know the expected slots it is very easy to create a default constructor. This is the second key contribution of this module, to provide a consistent and complete protocol for the construction and destruction of instances.

NOTE: The method documentation is meant to be read from top to bottom, in that they are documented in the same order they are called.

new ($class, @args)

This is the entry point for object construction, from here the @args are passed into BUILDARGS. The return value of new should always be a fully constructed and initialized instance.

BUILDARGS ($class, @args)

This method takes the original @args to the new constructor and is expected to turn them into a canonical form, which is a HASH ref of name/value pairs. This form is considered a prototype candidate for the instance, and subseqently what CREATE expects to receive.

NOTE: The values in the prototype candidate should be shallow copies of what was originally contained in @args, but this is not actually enforced, just suggested to provide better ownership distinctions.

CREATE ($class, $proto)

This method receives the $proto candidate from BUILDARGS and constructs from it a blessed instance of the class.

First it must call SLOTS (described above), then it will use the returned slot definitions along with the $proto candidate to construct a complete blessed instance. It does this by looping through the list of slots, using values in the $proto when available, otherwise using the slot initializers. The final blessed HASH ref based instance is then returned.

NOTE: If you wish to use a different instance type or some kind then this is the method you want to override.

BUILD ($self, $proto)

The newly blessed instance supplied by CREATE must still be initialized. We do this by calling all the available BUILD methods in the inheritance hierarchy in the correct (reverse mro) order.

BUILD is an optional initialization method which recieves the blessed instance as well as the prototype candidate. There are no restirctions as to what this method can do other then just common sense.

NOTE: It is worth noting that because we call all the BUILD methods found in the object hierarchy, the return values of these methods are completly ignored.


The last thing this module provides is an ordered destruction protocol for instances.

DEMOLISH ($self)

This is an optional destruction method, similar to BUILD, all available DEMOLISH methods are called in the correct (mro) order by DESTROY.

DESTROY ($self)

The sole function of this method is to kick off the call to all the DEMOLISH methods during destruction.


Stevan Little <stevan@cpan.org>


This software is copyright (c) 2016 by Stevan Little.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 172:

Non-ASCII character seen before =encoding in 'which, when'. Assuming UTF-8