View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Adriano Ferreira > Class-Lego-Constructor-0.004 > Class::Lego::Constructor



Annotate this POD

View/Report Bugs
Module Version: 0.004   Source  


Class::Lego::Constructor - Automated constructor generation


    require Class::Lego::Constructor;
    @ISA = qw( Class::Lego::Constructor );

    magical_number   => 42,
    timestamp => sub { DateTime->new }, # deferred
  # the weird name, mk_constructor0, is on purpose while API settles

    magical_number => { default => 42 },
    timestamp      => { default => sub { DateTime->new } },
    sort_function  => { default_value => sub { $a <=> $b } }, # really a sub


  NOTE: That module was formely called Class::Constructor::Factory.
  It will enter the Class-Lego dist soon.

I like the simplicity of Class::Accessor and friends. With them, creating accessors is a piece of cake. It does give me a default constructor as well.

The case is that this default constructor is not as convenient as the generated accessors. To be precise, I want to specify default values for attributes easily. This module is a tentative solution to this problem.


The intent of this module is to augment an existing constructor new which receives a hash ref argument containing pairs of field names versus their values.

  $obj = My::Class->new( { f1 => v1, f2 => v2 } );

When a declared field is omitted from the constructor argument, it can be filled with a default which was declared with mk_constructor0.



syntax highlighting: