line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Object::WithParams; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
125384
|
use warnings; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
128
|
|
4
|
4
|
|
|
4
|
|
17
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
125
|
|
5
|
4
|
|
|
4
|
|
22
|
use Carp qw/ croak /; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
1942
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=pod |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Object::WithParams - An Object With Params |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.3 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.3'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use Object::WithParams; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $thingy = Object::WithParams->new(); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# set a param |
28
|
|
|
|
|
|
|
$thingy->param(veggie => 'tomato'); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# get a param |
31
|
|
|
|
|
|
|
my $veggie = $thingy->param('veggie'); # $veggie eq 'tomato' |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# get all params |
34
|
|
|
|
|
|
|
my @params = $thingy->param(); # @params == ('veggie') |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# clone a Object::WithParams |
37
|
|
|
|
|
|
|
my $doodad = $thingy->clone; # $doodad->param('veggie') == 'tomato' |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# delete a param |
40
|
|
|
|
|
|
|
$thingy->delete('veggie'); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# delete all params |
43
|
|
|
|
|
|
|
$thingy->clear(); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Use this module to create objects that do nothing except contain parameters |
48
|
|
|
|
|
|
|
defined by you which you can get and set as you wish. Many modules such as |
49
|
|
|
|
|
|
|
L have methods that accept an object with a param() |
50
|
|
|
|
|
|
|
method and this object should be compatible with all of them. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This module really ought to be a role but there is no standardized way to |
53
|
|
|
|
|
|
|
do that in Perl 5. (Not everyone uses L.) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 METHODS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 new |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Creates a new, empty L. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $thingy = Object::WithParams->new; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new { |
66
|
2
|
|
|
2
|
1
|
14
|
my ($class) = @_; |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
7
|
my $self = {}; |
69
|
|
|
|
|
|
|
|
70
|
2
|
|
|
|
|
9
|
return bless $self, $class; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 clear |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Deletes all the extent parameters. Does not return anything. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$thingy->clear(); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub clear { |
82
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
2
|
foreach my $param ( keys %{$self} ) { |
|
1
|
|
|
|
|
4
|
|
85
|
3
|
|
|
|
|
8
|
delete $self->{$param}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
4
|
return; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 clone |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Returns a new L with the same set of parameters as the |
94
|
|
|
|
|
|
|
old one. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $doodad = $thingy->clone(); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub clone { |
101
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
7
|
my $clone = Object::WithParams->new(); |
104
|
|
|
|
|
|
|
|
105
|
1
|
|
|
|
|
4
|
foreach my $param ( $self->param() ) { |
106
|
3
|
|
|
|
|
12
|
$clone->param( $param => $self->param($param) ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
1
|
|
|
|
|
4
|
return $clone; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 delete |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Delete the named parameter. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$thingy->delete('veggie'); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub delete { ## no critic 'Subroutines::ProhibitBuiltinHomonyms' |
121
|
3
|
|
|
3
|
1
|
550
|
my ( $self, $param ) = @_; |
122
|
|
|
|
|
|
|
|
123
|
3
|
100
|
100
|
|
|
22
|
if ( defined $param && exists $self->{$param} ) { |
124
|
1
|
|
|
|
|
568
|
delete $self->{$param}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
3
|
|
|
|
|
8
|
return; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 param |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
The C method can be called in three ways. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=over 4 |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item with no arguments. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns a list of the parameters contained in the object. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my @params = $thingy->param(); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item with a single scalar argument. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
The value of the parameter with the name of the argument will be returned. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my $color = $thingy->param('color'); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item with named arguments |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
A parameter is created for one or more sets of keys and values. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$thingy->param(filename => 'logo.jpg', height => 50, width => 100); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
You could also use a hashref. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $arg_ref = { filename => 'logo.jpg', height => 50, width => 100 }; |
157
|
|
|
|
|
|
|
$thingy->param($arg_ref); |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
The value of a parameter need not be a scalar, it could be any any sort of |
160
|
|
|
|
|
|
|
reference even a coderef. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$thingy->param(number => &pick_a_random_number); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Does not return anything. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub param { |
171
|
24
|
|
|
24
|
1
|
1456
|
my ( $self, @args ) = @_; |
172
|
|
|
|
|
|
|
|
173
|
24
|
|
|
|
|
33
|
my $num_args = scalar @args; |
174
|
24
|
100
|
|
|
|
50
|
if ($num_args) { |
175
|
19
|
100
|
|
|
|
72
|
if ( ref $args[0] eq 'HASH' ) { # a hashref |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
176
|
1
|
|
|
|
|
2
|
%{$self} = ( %{$self}, %{ $args[0] } ); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
elsif ( $num_args % 2 == 0 ) { # a hash |
179
|
7
|
|
|
|
|
11
|
%{$self} = ( %{$self}, @args ); |
|
7
|
|
|
|
|
29
|
|
|
7
|
|
|
|
|
19
|
|
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
elsif ( $num_args == 1 ) { # a scalar |
182
|
10
|
|
|
|
|
57
|
return $self->{ $args[0] }; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
else { |
185
|
1
|
|
|
|
|
193
|
croak('Odd number of arguments passed to param().'); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
else { |
189
|
5
|
|
|
|
|
6
|
return keys %{$self}; |
|
5
|
|
|
|
|
48
|
|
190
|
|
|
|
|
|
|
} |
191
|
8
|
|
|
|
|
24
|
return; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 BUGS |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Not all possible param handling functionality is supported. Should it be? |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
199
|
|
|
|
|
|
|
C, or through the web interface at |
200
|
|
|
|
|
|
|
L. I will |
201
|
|
|
|
|
|
|
be notified, and then you'll automatically be notified of progress on your |
202
|
|
|
|
|
|
|
bug as I make changes. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 SUPPORT |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
perldoc Object::WithParams |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
You can also look for information at: |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=over 4 |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item * CPAN Ratings |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
L |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=item * Search CPAN |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
L |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=back |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 AUTHOR |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Jaldhar H. Vyas, Ejaldhar at braincells.comE |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 COPYRIGHT |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Copyright (C) 2010, Consolidated Braincells Inc. All Rights Reserved. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
This distribution is free software; you can redistribute it and/or modify it |
242
|
|
|
|
|
|
|
under the terms of either: |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
a) the GNU General Public License as published by the Free Software |
245
|
|
|
|
|
|
|
Foundation; either version 2, or (at your option) any later version, or |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
b) the Artistic License version 2.0. |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
The full text of the license can be found in the LICENSE file included |
250
|
|
|
|
|
|
|
with this distribution. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=cut |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
1; |
255
|
|
|
|
|
|
|
|