| 1 | package Class::FakeAttributes; | ||
| 2 | 1 | our $VERSION = 0.01; | |
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Class::FakeAttributes - Provide fake attributes for non-hash-based objects | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | use base qw<Class::FakeAttributes Some::Other::Module>; | ||
| 11 | |||
| 12 | sub something | ||
| 13 | { | ||
| 14 | my ($self, $whatever) = @_; | ||
| 15 | |||
| 16 | $self->set_attribute(motto => $whatever); | ||
| 17 | my $size = $self->get_attribute('size'); | ||
| 18 | # etc | ||
| 19 | |||
| 20 | =head1 WARNING | ||
| 21 | |||
| 22 | This is version 0.01. It exists for discussion. Do not rely on it. | ||
| 23 | Everything is subject to change, including the module's functionality, API, | ||
| 24 | name, and even its existence. Comments are welcome on the | ||
| 25 | S<module-authors@perl.org> mailing list. | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Most Perl classes use hash-based objects, and subclasses can easily add more | ||
| 30 | attributes (instance data) with new hash keys. But some classes are not based | ||
| 31 | on hashes. A subclass of such a class can use C<Class::FakeAttributes> to add | ||
| 32 | attributes (or at least to emulate doing so). | ||
| 33 | |||
| 34 | C<Class::FakeAttributes> is a mixin class: the only sensible use is to inherit | ||
| 35 | from it, and it only makes sense to do that when also inheriting from something | ||
| 36 | else as well. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | |||
| 43 | 1 | ▶ | use NEXT; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | |||
| 45 | |||
| 46 | # global hash for all attributes of all objects that use this (regardless of | ||
| 47 | # their class), keyed by the stringification of objects' blessed references: | ||
| 48 | 1 | our %attribute; | |
| 49 | |||
| 50 | =head1 METHODS | ||
| 51 | |||
| 52 | =head2 C<set_attribute()> | ||
| 53 | |||
| 54 | Use C<set_attribute()> to set an attribute on an object. Where with a | ||
| 55 | hash-based object you would have written: | ||
| 56 | |||
| 57 | $self->{key} = $value; | ||
| 58 | |||
| 59 | instead write: | ||
| 60 | |||
| 61 | $self->set_attribute(key => $value); | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub set_attribute | ||
| 66 | 0 | ▶ | { |
|
Subroutine
set_attribute: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 67 | 0 | my ($self, $key, $val) = @_; | |
| 68 | |||
| 69 | 0 | $attribute{$self}{$key} = $val; | |
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | =head2 C<get_attribute()> | ||
| 74 | |||
| 75 | Get the value of an attribute (set by C<set_attribute()>) with | ||
| 76 | C<get_attribute()>. Instead of this hash-based code: | ||
| 77 | |||
| 78 | my $value = $self->{key}; | ||
| 79 | |||
| 80 | do: | ||
| 81 | |||
| 82 | my $value = $self->get_attribute('key'); | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub get_attribute | ||
| 87 | 0 | ▶ | { |
|
Subroutine
get_attribute: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 88 | 0 | my ($self, $key) = @_; | |
| 89 | |||
| 90 | 0 | $attribute{$self}{$key}; | |
| 91 | } | ||
| 92 | |||
| 93 | =head2 C<push_attribute()> | ||
| 94 | |||
| 95 | For an attribute that has a list of values, append to that list with | ||
| 96 | C<push_attribute()>. Instead of this hash-based code: | ||
| 97 | |||
| 98 | push @{$self->{key}}, $value; | ||
| 99 | |||
| 100 | do: | ||
| 101 | |||
| 102 | $self->push_attribute(key => $value); | ||
| 103 | |||
| 104 | Multiple values can be pushed at once: | ||
| 105 | |||
| 106 | $self->push_attribute(food => @fruit); | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub push_attribute | ||
| 111 | 0 | ▶ | { |
|
Subroutine
push_attribute: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 112 | 0 | my ($self, $key, @val) = @_; | |
| 113 | |||
| 114 | 0 | push @{$attribute{$self}{$key}}, @val; | |
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | =head2 C<attribute_list()> | ||
| 119 | |||
| 120 | Retrieve the list of all values for a key with C<attribute_list>. Instead of | ||
| 121 | this hash-based code: | ||
| 122 | |||
| 123 | foreach (@{$self->{key}) | ||
| 124 | |||
| 125 | do: | ||
| 126 | |||
| 127 | foreach ($self->attribute_list('key')) | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub attribute_list | ||
| 132 | 0 | ▶ | { |
|
Subroutine
attribute_list: not called CC=1 SCAR=1.2
Pod
documented
| |||
| 133 | 0 | my ($self, $key) = @_; | |
| 134 | |||
| 135 | # If $self doesn't have any attributes then don't complain, just return an | ||
| 136 | # empty list (same as if it has some attributes but just not any with key | ||
| 137 | # $key): | ||
| 138 | 1 | ▶ | no warnings 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 139 | 0 | @{$attribute{$self}{$key}}; | |
| 140 | } | ||
| 141 | |||
| 142 | =head1 MEMORY LEAKAGE | ||
| 143 | |||
| 144 | The memory used to store an object's attributes is freed in a C<DESTROY> method | ||
| 145 | provided by C<Class::FakeAttributes>. If C<DESTROY> doesn't get called then | ||
| 146 | memory will be leaked. The best way to ensure memory gets freed up properly is | ||
| 147 | to put C<Class::FakeAttributes> at the start of the inheritance list. That is, | ||
| 148 | don't do this: | ||
| 149 | |||
| 150 | use base qw<Class::FakeAttributes Some::Other::Module>; | ||
| 151 | |||
| 152 | do this: | ||
| 153 | |||
| 154 | use base qw<Some::Other::Module Class::FakeAttributes>; | ||
| 155 | |||
| 156 | C<Class::FakeAttributes> uses L<the C<NEXT> module|NEXT> to ensure that, so | ||
| 157 | long as it is listed first, any C<DESTROY> method in other superclasses will | ||
| 158 | also be invoked. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub DESTROY | ||
| 163 | 0 | ▶ | { |
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||
| 164 | 0 | my ($self) = @_; | |
| 165 | |||
| 166 | # Free up the memory used for the attributes of this object: | ||
| 167 | 0 | delete $attribute{$self}; | |
| 168 | |||
| 169 | # Invoke any other DESTROY() method that would've been called had this class | ||
| 170 | # not existed: | ||
| 171 | 0 | $self->NEXT::DISTINCT::DESTROY; | |
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | 1 | 1; | |
| 177 | |||
| 178 | =head1 AUTHOR | ||
| 179 | |||
| 180 | Smylers <smylers@cpan.org> | ||
| 181 | |||
| 182 | =head1 COPYRIGHT | ||
| 183 | |||
| 184 | E<169> Copyright Smylers 2003. All rights reserved. This module is software | ||
| 185 | libre. It may be used, redistributed, or modified under the terms of the | ||
| 186 | Artistic License (the unnumbered version that comes with Perl 5.6.1, among | ||
| 187 | others) or the GNU General Public License version 2. | ||