| 1 | package Object::Releaser; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | # debugging tools | ||||||||||
| 5 | # use Debug::ShowStuff ':all'; | ||||||||||
| 6 | # use Debug::ShowStuff::ShowVar; | ||||||||||
| 7 | |||||||||||
| 8 | # version | ||||||||||
| 9 | 1 | our $VERSION = '0.12'; | |||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Object::Releaser -- Remove properties from an object when the releaser goes out of scope. | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | Remove all hash reference elements: | ||||||||||
| 18 | |||||||||||
| 19 | $object = {a=>1, b=>2, c=>3}; | ||||||||||
| 20 | $releaser = Object::Releaser->new($object); | ||||||||||
| 21 | undef $releaser; | ||||||||||
| 22 | # object still exists but has no elements | ||||||||||
| 23 | |||||||||||
| 24 | Remove only hash reference elements a and b: | ||||||||||
| 25 | |||||||||||
| 26 | $object = {a=>1, b=>2, c=>3}; | ||||||||||
| 27 | $releaser = Object::Releaser->new($object); | ||||||||||
| 28 | $releaser->set_keys(qw{a b}); | ||||||||||
| 29 | undef $releaser; | ||||||||||
| 30 | # object has element c but not a and b | ||||||||||
| 31 | |||||||||||
| 32 | Cancel the release, don't release anything: | ||||||||||
| 33 | |||||||||||
| 34 | $object = {a=>1, b=>2, c=>3}; | ||||||||||
| 35 | $releaser = Object::Releaser->new($object); | ||||||||||
| 36 | $releaser->dismiss(); | ||||||||||
| 37 | undef $releaser; | ||||||||||
| 38 | # object is not changed | ||||||||||
| 39 | |||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||
| 41 | |||||||||||
| 42 | Object::Releaser provides the ability to delete all or some of the elements | ||||||||||
| 43 | from a hash reference when the releaser goes out of scope. This is done by | ||||||||||
| 44 | creating the releaser, passing in the object to be released as the sole | ||||||||||
| 45 | argument: | ||||||||||
| 46 | |||||||||||
| 47 | $releaser = Object::Releaser->new($object); | ||||||||||
| 48 | |||||||||||
| 49 | When $releaser goes out of scope, all elements in $object are deleted. | ||||||||||
| 50 | |||||||||||
| 51 | If you only want specific elements deleted, set those elements with | ||||||||||
| 52 | $releaser->set_keys(). So, for example, the following lines set the releaser | ||||||||||
| 53 | to delete elements a and b from the object, but not any other elements: | ||||||||||
| 54 | |||||||||||
| 55 | $releaser = Object::Releaser->new($object); | ||||||||||
| 56 | $releaser->set_keys(qw{a b}); | ||||||||||
| 57 | |||||||||||
| 58 | =head1 ALTERNATIVES | ||||||||||
| 59 | |||||||||||
| 60 | Object::Destroyer provides very similar functionality. It provides for more | ||||||||||
| 61 | complex situations and has greater flexibility. Object::Releaser fulfills one | ||||||||||
| 62 | simple function: deleting elements from a hashref. | ||||||||||
| 63 | |||||||||||
| 64 | If you just want to avoid circular references, you might want to use weaken in | ||||||||||
| 65 | the Scalar::Util module (which is built into Perl as of version 5.6.0). | ||||||||||
| 66 | |||||||||||
| 67 | =head1 INSTALLATION | ||||||||||
| 68 | |||||||||||
| 69 | Array::OneOf can be installed with the usual routine: | ||||||||||
| 70 | |||||||||||
| 71 | perl Makefile.PL | ||||||||||
| 72 | make | ||||||||||
| 73 | make test | ||||||||||
| 74 | make install | ||||||||||
| 75 | |||||||||||
| 76 | =head1 METHODS | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | #------------------------------------------------------------------------------ | ||||||||||
| 83 | # new | ||||||||||
| 84 | # | ||||||||||
| 85 | |||||||||||
| 86 | =head2 new | ||||||||||
| 87 | |||||||||||
| 88 | Creates an Object::Releaser object. The single argument is the object to be | ||||||||||
| 89 | released when $releaser goes out of scope: | ||||||||||
| 90 | |||||||||||
| 91 | $releaser = Object::Releaser->new($object); | ||||||||||
| 92 | |||||||||||
| 93 | If you do nothing else, then all elements in $object will be deleted when | ||||||||||
| 94 | $releaser goes out of scope. | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 99 | 6 | my ($class, $object) = @_; | |||||||||
| 100 | 6 | my $releaser = bless {}, $class; | |||||||||
| 101 | |||||||||||
| 102 | # store object | ||||||||||
| 103 | 6 | $releaser->{'object'} = $object; | |||||||||
| 104 | |||||||||||
| 105 | # return | ||||||||||
| 106 | 6 | return $releaser; | |||||||||
| 107 | } | ||||||||||
| 108 | # | ||||||||||
| 109 | # new | ||||||||||
| 110 | #------------------------------------------------------------------------------ | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | #------------------------------------------------------------------------------ | ||||||||||
| 114 | # set_keys | ||||||||||
| 115 | # | ||||||||||
| 116 | |||||||||||
| 117 | =head2 set_keys | ||||||||||
| 118 | |||||||||||
| 119 | Tells the releaser to only delete specified keys from the object. For example, | ||||||||||
| 120 | the code: | ||||||||||
| 121 | |||||||||||
| 122 | $releaser->set_keys(qw{a b}); | ||||||||||
| 123 | |||||||||||
| 124 | sets the releaser so that only elements C<a> and C<b> are deleted. | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | 2 | ▶ | sub set_keys { | ||||||||
|
Subroutine
set_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 129 | 2 | my ($releaser, @keys) = @_; | |||||||||
| 130 | 2 | $releaser->{'keys'} = \@keys; | |||||||||
| 131 | } | ||||||||||
| 132 | # | ||||||||||
| 133 | # set_keys | ||||||||||
| 134 | #------------------------------------------------------------------------------ | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | |||||||||||
| 138 | #------------------------------------------------------------------------------ | ||||||||||
| 139 | # delete_all | ||||||||||
| 140 | # | ||||||||||
| 141 | |||||||||||
| 142 | =head2 delete_all | ||||||||||
| 143 | |||||||||||
| 144 | C<delete_all> does the opposite of C<set_keys>: it sets the releaser to delete | ||||||||||
| 145 | all keys from the target object. Use C<delete_all> if you previously used | ||||||||||
| 146 | C<set_keys> to set deletion for specific keys, but now want to go back to | ||||||||||
| 147 | deleting all keys: | ||||||||||
| 148 | |||||||||||
| 149 | $releaser = Object::Releaser->new($object); | ||||||||||
| 150 | $releaser->set_keys(qw{a b}); | ||||||||||
| 151 | $releaser->delete_all(); | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | 1 | ▶ | sub delete_all { | ||||||||
|
Subroutine
delete_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 156 | 1 | my ($releaser) = @_; | |||||||||
| 157 | 1 | delete $releaser->{'keys'}; | |||||||||
| 158 | } | ||||||||||
| 159 | # | ||||||||||
| 160 | # delete_all | ||||||||||
| 161 | #------------------------------------------------------------------------------ | ||||||||||
| 162 | |||||||||||
| 163 | |||||||||||
| 164 | |||||||||||
| 165 | #------------------------------------------------------------------------------ | ||||||||||
| 166 | # dismiss | ||||||||||
| 167 | # | ||||||||||
| 168 | 3 | ▶ | sub dismiss { | ||||||||
|
Subroutine
dismiss: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 169 | 3 | my ($releaser, $dismiss) = @_; | |||||||||
| 170 | |||||||||||
| 171 | # if $dismiss is defined, use that value | ||||||||||
| 172 | 3 | ▶ | if (defined $dismiss) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 2 | { $releaser->{'dismiss'} = $dismiss } | |||||||||
| 174 | |||||||||||
| 175 | # else set to true | ||||||||||
| 176 | else | ||||||||||
| 177 | 1 | { $releaser->{'dismiss'} = 1 } | |||||||||
| 178 | |||||||||||
| 179 | # return | ||||||||||
| 180 | 3 | return $releaser->{'dismiss'}; | |||||||||
| 181 | } | ||||||||||
| 182 | # | ||||||||||
| 183 | # dismiss | ||||||||||
| 184 | #------------------------------------------------------------------------------ | ||||||||||
| 185 | |||||||||||
| 186 | |||||||||||
| 187 | |||||||||||
| 188 | #------------------------------------------------------------------------------ | ||||||||||
| 189 | # DESTROY | ||||||||||
| 190 | # | ||||||||||
| 191 | 6 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=6 SCAR=17.9
| |||||||||||
| 192 | 6 | my ($releaser) = @_; | |||||||||
| 193 | 6 | my $object = $releaser->{'object'}; | |||||||||
| 194 | |||||||||||
| 195 | # if no object, return | ||||||||||
| 196 | 6 | ▶ | $object or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | |||||||||||
| 198 | # if dismissed, return | ||||||||||
| 199 | 6 | ▶ | if ($releaser->{'dismiss'}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | 2 | { return } | |||||||||
| 201 | |||||||||||
| 202 | # if keys are defined, delete only those keys | ||||||||||
| 203 | 4 | ▶ | if ($releaser->{'keys'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 204 | 1 | foreach my $key (@{$releaser->{'keys'}}) | |||||||||
| 205 | 2 | { delete $object->{$key} } | |||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | # else release all properties | ||||||||||
| 209 | else { | ||||||||||
| 210 | 3 | foreach my $key (keys %$object) | |||||||||
| 211 | 9 | { delete $object->{$key} } | |||||||||
| 212 | } | ||||||||||
| 213 | } | ||||||||||
| 214 | # | ||||||||||
| 215 | # DESTROY | ||||||||||
| 216 | #------------------------------------------------------------------------------ | ||||||||||
| 217 | |||||||||||
| 218 | |||||||||||
| 219 | |||||||||||
| 220 | # return true | ||||||||||
| 221 | 1 | 1; | |||||||||
| 222 | |||||||||||
| 223 | __END__ | ||||||||||