line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Object::Releaser; |
2
|
1
|
|
|
1
|
|
736
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
342
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# debugging tools |
5
|
|
|
|
|
|
|
# use Debug::ShowStuff ':all'; |
6
|
|
|
|
|
|
|
# use Debug::ShowStuff::ShowVar; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# version |
9
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub new { |
99
|
6
|
|
|
6
|
1
|
243
|
my ($class, $object) = @_; |
100
|
6
|
|
|
|
|
20
|
my $releaser = bless {}, $class; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# store object |
103
|
6
|
|
|
|
|
15
|
$releaser->{'object'} = $object; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# return |
106
|
6
|
|
|
|
|
13
|
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 and C are deleted. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub set_keys { |
129
|
2
|
|
|
2
|
1
|
9
|
my ($releaser, @keys) = @_; |
130
|
2
|
|
|
|
|
5
|
$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 does the opposite of C: it sets the releaser to delete |
145
|
|
|
|
|
|
|
all keys from the target object. Use C if you previously used |
146
|
|
|
|
|
|
|
C 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
|
|
|
|
|
|
|
sub delete_all { |
156
|
1
|
|
|
1
|
1
|
4
|
my ($releaser) = @_; |
157
|
1
|
|
|
|
|
4
|
delete $releaser->{'keys'}; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
# |
160
|
|
|
|
|
|
|
# delete_all |
161
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
166
|
|
|
|
|
|
|
# dismiss |
167
|
|
|
|
|
|
|
# |
168
|
|
|
|
|
|
|
sub dismiss { |
169
|
3
|
|
|
3
|
0
|
9
|
my ($releaser, $dismiss) = @_; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# if $dismiss is defined, use that value |
172
|
3
|
100
|
|
|
|
7
|
if (defined $dismiss) |
173
|
2
|
|
|
|
|
4
|
{ $releaser->{'dismiss'} = $dismiss } |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# else set to true |
176
|
|
|
|
|
|
|
else |
177
|
1
|
|
|
|
|
3
|
{ $releaser->{'dismiss'} = 1 } |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# return |
180
|
3
|
|
|
|
|
5
|
return $releaser->{'dismiss'}; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
# |
183
|
|
|
|
|
|
|
# dismiss |
184
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
189
|
|
|
|
|
|
|
# DESTROY |
190
|
|
|
|
|
|
|
# |
191
|
|
|
|
|
|
|
sub DESTROY { |
192
|
6
|
|
|
6
|
|
28
|
my ($releaser) = @_; |
193
|
6
|
|
|
|
|
8
|
my $object = $releaser->{'object'}; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# if no object, return |
196
|
6
|
50
|
|
|
|
13
|
$object or return; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# if dismissed, return |
199
|
6
|
100
|
|
|
|
22
|
if ($releaser->{'dismiss'}) |
200
|
2
|
|
|
|
|
8
|
{ return } |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# if keys are defined, delete only those keys |
203
|
4
|
100
|
|
|
|
9
|
if ($releaser->{'keys'}) { |
204
|
1
|
|
|
|
|
2
|
foreach my $key (@{$releaser->{'keys'}}) |
|
1
|
|
|
|
|
2
|
|
205
|
2
|
|
|
|
|
6
|
{ delete $object->{$key} } |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# else release all properties |
209
|
|
|
|
|
|
|
else { |
210
|
3
|
|
|
|
|
9
|
foreach my $key (keys %$object) |
211
|
9
|
|
|
|
|
23
|
{ delete $object->{$key} } |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
# |
215
|
|
|
|
|
|
|
# DESTROY |
216
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# return true |
221
|
|
|
|
|
|
|
1; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
__END__ |