Class::FakeAttributes - Provide fake attributes for non-hash-based objects
use base qw<Class::FakeAttributes Some::Other::Module>; sub something { my ($self, $whatever) = @_; $self->set_attribute(motto => $whatever); my $size = $self->get_attribute('size'); # etc
This is version 0.01. It exists for discussion. Do not rely on it. Everything is subject to change, including the module's functionality, API, name, and even its existence. Comments are welcome on the module-authors@perl.org mailing list.
Most Perl classes use hash-based objects, and subclasses can easily add more attributes (instance data) with new hash keys. But some classes are not based on hashes. A subclass of such a class can use Class::FakeAttributes to add attributes (or at least to emulate doing so).
Class::FakeAttributes
Class::FakeAttributes is a mixin class: the only sensible use is to inherit from it, and it only makes sense to do that when also inheriting from something else as well.
set_attribute()
Use set_attribute() to set an attribute on an object. Where with a hash-based object you would have written:
$self->{key} = $value;
instead write:
$self->set_attribute(key => $value);
get_attribute()
Get the value of an attribute (set by set_attribute()) with get_attribute(). Instead of this hash-based code:
my $value = $self->{key};
do:
my $value = $self->get_attribute('key');
push_attribute()
For an attribute that has a list of values, append to that list with push_attribute(). Instead of this hash-based code:
push @{$self->{key}}, $value;
$self->push_attribute(key => $value);
Multiple values can be pushed at once:
$self->push_attribute(food => @fruit);
attribute_list()
Retrieve the list of all values for a key with attribute_list. Instead of this hash-based code:
attribute_list
foreach (@{$self->{key})
foreach ($self->attribute_list('key'))
The memory used to store an object's attributes is freed in a DESTROY method provided by Class::FakeAttributes. If DESTROY doesn't get called then memory will be leaked. The best way to ensure memory gets freed up properly is to put Class::FakeAttributes at the start of the inheritance list. That is, don't do this:
DESTROY
use base qw<Class::FakeAttributes Some::Other::Module>;
do this:
use base qw<Some::Other::Module Class::FakeAttributes>;
Class::FakeAttributes uses the NEXT module to ensure that, so long as it is listed first, any DESTROY method in other superclasses will also be invoked.
NEXT
Smylers <smylers@cpan.org>
© Copyright Smylers 2003. All rights reserved. This module is software libre. It may be used, redistributed, or modified under the terms of the Artistic License (the unnumbered version that comes with Perl 5.6.1, among others) or the GNU General Public License version 2.
To install Class::FakeAttributes, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::FakeAttributes
CPAN shell
perl -MCPAN -e shell install Class::FakeAttributes
For more information on module installation, please visit the detailed CPAN module installation guide.