NAME

Class::Classless::C3 - Classless object system framework

SYNOPSIS

use Class::Classless::C3;

# create a new object
my $a = Class::Classless::C3::ROOT->new('a');
my $b = Class::Classless::C3->new('b');   # ROOT is default parent

# create derived object
my $c = $b->new('c');

# attributes (not inherited) and methods (inherited)
$b->{'attribute'} = 'exists';
$b->meta->addmethod('method' => sub { "exists" });

print $c->{'attribute'};  # ''
print $c->method;         # 'exists'

DESCRIPTION

This implements a classless object system, very similar to Class::Classless.

There are two major differences. One is that Class::Classless::C3 relies on Algorithm::C3 to determine the inheritance tree, which outsources the most complicated part of the code.

The more important difference is that there is no $callstate object passed around in Class::Classless::C3. This means methods can be written in exactly the same way methods are written in other perl objects. The job formerly done by $callstate is now accomplished using Sub::Name.

Classless Objects

As with Class::Classless, all objects are created by inheriting from a $ROOT object. Objects can have attributes (data members) which are not inherited, and can have methods added to them, which will be inherited by sub-objects.

A Class::Classless::C3 object is a blessed hash reference, so attributes are merely hash entries.

The only "special" hash key is called _meta, but you should use the meta accessor method. It contains a classless meta object which contains all the meta information for the class, including its name, parent(s), and methods. It is also used when altering the class meta information.

OBJECT METHODS

$obj2 = $obj->new( ['name'] )

Creates a new $obj2, with the given name, with $obj as the parent. This is like the 'clone' method from Class::Classless. If no name is given, and unique name will be autogenerated. It calls an 'init' method, which you can override for object initialization.

$self->NEXT(@_);

This is how to call the same method in the superclass. It will dispatch in C3 method resolution order.

It is similar to the use of SUPER:: in normal perl objects, and equivalent to next::method from Class::C3.

It is a no-op if the method does not exist in any superclass.

$obj->can( 'methodname' )

Checks whether an object (or its parents) can execute the given method, and returns a code reference for the method.

$obj->isa( $objn )

Returns true if $obj is a descendant of the argument, which can be an object or an object name. The 'can' method is more useful.

$obj->VERSION

Should return a version number, if your object has one.

$obj->meta

Returns the meta-object, used for altering the object meta-information, such as the object name, parent object(s), and adding methods.

OBJECT META METHODS

$obj->meta->name( ['name'] )

Gets or sets the object's name.

$obj->meta->parent( [$obj2] )

Gets or sets the object's parent. This method only supports single inheritance.

$obj->meta->parents( [$obj3, $obj4, ...] )

Gets or sets multiple parents for an objects, in order to support multiple inheritance.

$obj->meta->addparent( $obj5 )

Adds another parent to the object. This is the most common method of setting up multiple inheritance.

$obj->meta->addmethod( 'mymethod' => sub { 'this is what i do' } )

Adds a new method to an object. Requires a method name and a code ref.

$obj->meta->delmethod( 'mymethod' )

Deletes a method from an object.

Miscellany

These are some extra features provided by Class::Classless::C3. I'm not sure why you would use them, but I found a use for them.

Class::Classless::C3::Meta::declassify

This is a convenience method to create a classless object from an existing perl package. The statement

$parent = Package->Class::Classless::C3:Meta::declassify();

will create a classless object with all the methods defined in 'Package', which you could use as a parent for a lot of other generated classless objects. Mainly, it makes writing the code for the parent object more straight-forward and perlish.

$Class::Classless::C3::autoload

If you assign a coderef to this variable, it will activate an autoload-like feature.

If $obj->method is called, and method does not exist, this autoload sub is called (with the object and methodname as arguments) it give it a chance to create the method. It should return a coderef on success, which will be called. It can also do $obj->meta->addmethod to avoid this overhead in the future.

$Class::Classless::C3::trace

If this is assigned to a scalar ref (not a scalar), every time a classless method or NEXT is called, debug information will be appended to the string.

NOTES

All classless objects are blessed in to the Class::Classless:C3 namespace, so to determine if an object is classless, just ask whether ref($obj) eq 'Class::Classless::C3'

Algorithm::C3 will die on invalid inheritance trees. The algorithm used in Class::Classless would try anyway, not necessarily with sane results.

SEE ALSO

The original Class::Classless

AUTHOR

John Williams, smailliw@gmail.com

COPYRIGHT AND LICENSE

Copyright (C) 2008-2010 by John Williams. All rights reserved.

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