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") { $object->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) = @_; $self->set_name($name); $self->set_rank($rank); $self->set_serial_number($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>
<alex at gmail dot com>
Please report any bugs or feature requests to bug-object-new at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-New. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-object-new at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Object::New
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Object-New
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Object-New
CPAN Ratings
http://cpanratings.perl.org/d/Object-New
Search CPAN
http://search.cpan.org/dist/Object-New/
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 http://dev.perl.org/licenses/ for more information.
To install Object::New, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Object::New
CPAN shell
perl -MCPAN -e shell install Object::New
For more information on module installation, please visit the detailed CPAN module installation guide.