NAME
ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
SYNOPSIS
package Foo;
use metaclass 'ClassEncapsulatedAttributes';
Foo->meta->add_attribute('foo' => (
accessor => 'Foo_foo',
default => 'init in FOO'
));
sub new {
my $class = shift;
$class->meta->new_object(@_);
}
package Bar;
our @ISA = ('Foo');
# duplicate the attribute name here
Bar->meta->add_attribute('foo' => (
accessor => 'Bar_foo',
default => 'init in BAR'
));
# ... later in other code ...
my $bar = Bar->new();
prints $bar->Bar_foo(); # init in BAR
prints $bar->Foo_foo(); # init in FOO
# and ...
my $bar = Bar->new(
'Foo' => { 'foo' => 'Foo::foo' },
'Bar' => { 'foo' => 'Bar::foo' }
);
prints $bar->Bar_foo(); # Foo::foo
prints $bar->Foo_foo(); # Bar::foo
DESCRIPTION
This is an example metaclass which encapsulates a class's attributes on a per-class basis. This means that there is no possibility of name clashes with inherited attributes. This is similar to how C++ handles its data members.
ACKNOWLEDGEMENTS
Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
AUTHORS
Stevan Little <stevan@iinteractive.com>
Yuval Kogman <nothingmuch@woobling.com>
COPYRIGHT AND LICENSE
Copyright 2006-2008 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.