Author image Richard Soderberg

NAME

Class::Init - A base constructor class with support for local initialization methods.

SYNOPSIS

  package Something::Spiffy;
  use base Class::Init;
  sub _init {
    my $self = shift;
    exists $self->{dsn} || die "parameter 'dsn' missing";
    $self->{_dbh} = DBI->connect($self->{dsn}) || die "DBI->connect failed";
  }
  sub users {
    my $sth = shift->{_dbh}->prepare_cached("SELECT * FROM users");
    $sth->execute(@_); return @{ $sth->fetchall_arrayref };
  }

  package main;
  my $database = Something::Spiffy->new( dsn => '...' );
  my @users = $database->users;
  ...

DESCRIPTION

Class::Init provides a constructor, new(), that returns blessed hashrefs by default; that constructor runs all instances of the subroutine _init it finds in the inheritance tree, top-down (EVERY).

The goal of this module is to reduce the amount of effort required to construct a simple object class; it helps reduce the amount of code that's duplicated between classes by providing a generic constructor, while allowing individual classes a low-effort way to publish their own changes to the new object.

AUTHOR

Richard Soderberg, <perl@crystalflame.net>

COPYRIGHT AND LICENSE

Copyright 2004 by Richard Soderberg

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