The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Object::New - A default constructor for standard objects


Version 0.02


When you want Moose, there is Moose, but when you just want to write "standard" Perl5 OO code, there is a bit of boilerplate, and "->new" is one bit of it. This module just does just one thing: provide a sane default constructor you can use for standard Perl5 objects. (A standard object is here taken to be a blessed hash.)

If you want anything else, there is lots out there: Moose, obviously, but also Object::Tiny, and Rose::Object and many others. This modules is simply an exercise in code reuse, and an attempt to avoid a common piece of boilerplate.


The new routine in all its glory is:

    sub new {
        my $class = shift;
        my $object = {};
        bless $object, $class;
        if $object->can("init") {
        return $object;

So you get a blessed hash at the end. If your code has a init routine defined that an object in its namespace can access, it will call init as well, with the argument list passed to new.

To customise object construction, all you have to do is define an init routine:

  package POW;

  use Object::New;
  use feature "say";

  sub init {
    my $self = shift;
    my ($name, $rank, $serial_number) = @_;

  # attribute accessors defined here...

  sub interrogate {
    my $self = shift;
    say join(", ", $self->name, $self->rank, $self->serial_number);


This module exports one routine by default: "new"

If you don't want to import this, you don't want to be using this module.



A default constructor. This returns a reference to a hash, blessed into its invoking class. If an init method is available, it is called with the argument list passed to the constructor.


Alex Kalderimis, <alex at gmail dot com>


Please report any bugs or feature requests to bug-object-new at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc Object::New

You can also look for information at:



Copyright 2011 Alex Kalderimis.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.