NAME

Coat -- a meta class for building light objects with accessors

DESCRIPTION

This module was inspired by the excellent Moose meta class which provides enhanced object creation for Perl 5.

Moose is great, but slow and has huge dependencies which makes it difficult to use in restricted environments.

This module implements the basic goodness of Moose, namely the accessor automagic.

It is not Moose

It is designed for developers who want to write clean object code with Perl 5 without depending on Moose. This implies you don't rely on all the features of Moose; and you don't depend on a huge set of dependencies, all you have to install is Coat (which is independant, no need of external modules).

SYNTAX

package Point;
use Coat;  # once the use is done, the class already 
           # inherits from it

var 'x' => (type => 'Int', default => 0);
var 'y' => (type => 'Int', default => 0);

1;

my $point = new Point x => 2, y => 4;
$point->x;    # returns 2
$point->y;    # returns 4
$point->x(9); # returns 9

Note that we don't need to import "strict" and "warnings" modules as Coat propagates them to our class (use strict and use warnings are implicit in our class).

STATIC METHODS

var 'name' => %options

The static method var allows you to define attributes for your class. Attributes declared this way will be available in the objects (accessors will let you get and set it).

This static method is similar to Moose's has method.

You can handle each attribute options with the %options hashtable. The following options are supported:

type

The attribute's type, put here either a class name or one of the supported type:

Int
String
Boolean

default

The attribute's default value (the attribute will have this value at instanciation time if none given).

extends

The keyword "extends" allows you to declare that a class "foo" inherits from a class "bar". All attributes properties of class "bar" will be applied to class "foo" as well as the accessors of class "bar".

Here is an example with Point3D, an extension of Point previously declared in this documentation:

package Point3D;

use Coat;
extends 'Point';

var 'z' => (type => 'Int', default => 0):

my $point3d = new Point3D x => 1, y => 3, z => 1;
$point3d->x;    # will return: 1
$point3d->y;    # will return: 3
$point3d->z;    # will return: 1

HOOKS

Like Moose, Coat lets you define hooks. There are three kind of hooks : before, after and around.

before

When writing a "before" hook you can catch the call to an inherited method, and execute some code before the inherited method is called.

Example:

package Foo;
use Coat;

sub method { return 4; }

package Bar;
use Coat;
extends 'Foo';

around 'method' => sub {
  my ($self, @args) = @_;
  # ... here some stuff to do before Foo::method is called
};

after

When writing an "after" hook you can catch the call to an inherited method and execute xome code after the original method is executed. You receive in your hook the result of the father's method.

Example:

package Foo;
use Coat;

sub method { return 4; }

package Bar;
use Coat;
extends 'Foo';

after 'method' => sub {
  my ($self, $result, @args) = @_;
  return $result + 3;
};

around

When writing an "around" hook you can catch the call to an inherited method and actually redefine it on-the-fly. You get the code reference to the parent's method and its arguments, and can do what you want then. It's a very powerful hook but also dangerous, so be careful when writing such a hook not to break the original call.

Example:

package Foo;
use Coat;

sub method { return 4; }

package Bar;
use Coat;
extends 'Foo';

# the following around hook implement the previous 'after' hook 
# defined in this documentaiton.

around 'method' => sub {
  my $orig = shift;
  my ($self, @args) = @_;

  my $res = $self->$orig(@args);
  return $res + 3;
}

AUTHOR

Coat was written by Alexis Sukrieh <sukria+perl@sukria.net>

COPYING

Coat is copyright (c) 2007 Alexis Sukrieh.

http://www.sukria.net/perl/coat/

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

1 POD Error

The following errors were encountered while parsing the POD:

Around line 488:

=back without =over