ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
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
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.
Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
Stevan Little <stevan@iinteractive.com>
Yuval Kogman <nothingmuch@woobling.com>
Copyright 2006-2008 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Moose
CPAN shell
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.