NAME

Class::Monadic - Provides monadic methods (a.k.a. singleton methods)

VERSION

This document describes Class::Monadic version 0.01.

SYNOPSIS

use Class::Monadic;

my $dbh1 = DBI->connect(...);

Class::Monadic->initialize($dbh1)->add_method(
	foo => sub{ ... },
);

$dbh1->foo(...); # OK

my $dbh2 = DBI->connect(...);

$dbh2->foo(); # throws "Can't locate object method ..."
              # because foo() is $dbh1 specific.

# import a syntax sugar to make an object monadic
use Class::Monadic qw(monadic);

monadic($dbh1)->inject_base(qw(SomeComponent OtherComponent));
# now $dbh1 is-a both SomeComponent and OtherComponent

monadic($dbh1)->add_field(qw(x y z));
$dbh1->set_x(42);
print $dbh->get_x(); # => 42

DESCRIPTION

Class::Monadic provides per-object classs, monadic classes. It is also known as singleton classes in other languages, e.g. Ruby.

Monadic classes is used in order to define monadic methods (a.k.a. singleton methods), which are only available at the specific object they are defined into.

INTERFACE

Exportable functions

monadic($object)

A syntax sugar to Class::Monadic->initialize($object).

Class methods

Class::Monadic->initialize($object)

Makes $object monadic, and returns Class::Monadic instance, $meta.

Instance methods

$meta->name

Returns the name of the monadic class.

$meta->add_method(%name_code_pairs)

Adds methods into the monadic class.

$meta->add_field(@field_names)

Adds fields and accessors named get_$name/set_$name into the monadic class.

Fields are not stored in the object. Rather, stored in the monadic class.

$meta->add_modifier($type, @method_names, $code)

Adds method modifiers to specific methods, Using Class::Method::Modifiers::Fast.

$type is must be before, around or after.

Example:

monadic($obj)->add_modifier(before => foo => sub{ ... });
monadic($obj)->add_modifier(around => qw(foo bar baz),
	sub{
		my $next = shift;
		my(@args) = @_;
		# ...
		return &{$next};
	}
);
monadic($obj)->add_modifier(after => xyzzy => sub{ ... });

$meta->inject_base(@component_classes)

Adds @component_classes into the is-a hierarchy of the monadic class.

DEPENDENCIES

Perl 5.8.1 or later.

Data::Util.

Hash::FieldHash.

BUGS

No bugs have been reported.

Please report any bugs or feature requests to the author.

SEE ALSO

Class::MOP.

AUTHOR

Goro Fuji (gfx) <gfuji(at)cpan.org>.

LICENSE AND COPYRIGHT

Copyright (c) 2009, Goro Fuji (gfx). Some rights reserved.

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