1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
package Class::NamedParms;
# $RCSfile: NamedParms.pm,v $ $Revision: 1.1 $ $Date: 1999/06/15 17:25:38 $ $Author: snowhare $
use strict;
use Carp;
use vars qw($VERSION);
$VERSION = '1.04';
=head1 NAME
Class::NamedParms - A lightweight named parameter handling system.
=head1 SYNOPSIS
package SomePackage;
use Class::NamedParms;
use vars qw (@ISA);
@ISA=qw(Class::NamedParms);
sub new {
my ($proto) = shift;
my ($class) = ref ($proto) || $proto;
$self = Class::NamedParms->new(-benefits,-costs);
$self = bless $self,$class;
$self;
}
$thingy = SomePackage->new;
$thingy->set({ -benefits => 1, -costs => 0.5 });
my ($costs,$benefits) = $thingy->get(-costs,-benefits);
=head1 DESCRIPTION
Provides key name checking for named accessor parameters. This allows
the use of a generic 'get/set' type parameterized accessor while
automatically catching accidental mis-spellings and usage of uninitialized
parameters. This catches a large class of programming errors without requiring
a new accessor for every object parameter.
=head1 CHANGES
1.00 1999.06.16 - Initial release.
1.01 1999.06.17 - Bug fix to 'clear' method. Added 'make test' support.
1.02 1999.06.18 - Performance tweak to 'get' method.
1.03 1999.06.21 - Minor docs tweaks. Removal of 'use attrs' for portability
1.04 1999.10.08 - Bug fix to 'all_parms' method
=head2 Initialization
=cut
######################################################################
=over 4
=item C<new;>
Creates a new instance of a NamedParms object.
You can optionally 'declare' the legal parameter keys at the same time.
Example:
my $self = Class::NamedParms(-benefits,-costs,-other);
=back
=cut
sub new {
my ($class) = shift;
my $self = bless {},$class;
$self->{-legal_parms} = {};
$self->{-parm_values} = {};
if ($#_ != -1) {
$self->declare(@_);
}
$self;
}
######################################################################
=over 4
=item C<list_declared_parms;>
Returns a list of all parm names that have been declared for this
NamedParms object. List is unsorted.
=back
=cut
sub list_declared_parms {
my $self = shift;
my (@parmnames) = keys %{$self->{-legal_parms}};
return @parmnames;
}
######################################################################
=over 4
=item C<list_initialized_parms;>
Lists all parms that have had values initialized for this NamedParms object
Returns a list of the parameter names. List is unsorted.
=back
=cut
sub list_initialized_parms {
my $self = shift;
my (@parmnames) = keys %{$self->{-parm_values}};
return @parmnames;
}
######################################################################
=over 4
=item C<declare($parmname,[$parmname1,...]);>
Declares one or more parameters for use with the NamedParms object.
Example:
$self->declare(-moved_in,-car_key,-house_key,-relationship);
This *does not* initialize the parameters - only declares them
to be legal for use.
=back
=cut
sub declare {
my $self = shift;
my (@parmnames) = @_;
my $parmname;
foreach $parmname (@parmnames) {
$parmname = lc ($parmname);
$self->{-legal_parms}->{$parmname} = 1;
}
}
######################################################################
=over 4
=item C<undeclare($parmname,$parmname1,...);>
'undeclares' one or more parameters for use with the NamedParms object.
This also deletes any values assigned to those parameters.
Example:
$self->undeclare(-house_key,-car_key,-relationship);
=back
=cut
sub undeclare {
my $self = shift;
my (@parmnames) = @_;
my $parmname;
foreach $parmname (@parmnames) {
$parmname = lc ($parmname);
if (exists $self->{-legal_parms}->{$parmname}) {
delete $self->{-legal_parms}->{$parmname};
if (exists $self->{-parm_values}->{$parmname}) {
delete $self->{-parm_values}->{$parmname};
}
} else {
confess (__PACKAGE__ . "::undeclare() - Attempted to undeclare a parameter name ($parmname) that was never declared\n");
}
}
}
######################################################################
=over 4
=item C<exists($parmname);>
Returns true if the specified parmname has been initialized via 'set'.
=back
=cut
sub exists {
my $self = shift;
my ($parmname) = @_;
$parmname = lc $parmname;
# The Perl built-in, not us.
CORE::exists $self->{-parm_values}->{$parmname};
}
######################################################################
=over 4
=item C<set($parm_ref);>
Sets one or more named parameter values.
Example:
$self->set({ -thingy => 'test', -other_thingy => 'more stuff' });
Will 'confess' if an attempt is made to set an undeclared parameter key.
=back
=cut
sub set {
my $self = shift;
my $parm_ref = {};
if ($#_ == 0) {
$parm_ref = shift;
} elsif ($#_ > 0) {
%$parm_ref = @_;
}
my (@parmnames) = keys %$parm_ref;
my $parmname;
foreach $parmname (@parmnames) {
my $keyname = lc ($parmname);
my $value = $parm_ref->{$parmname};
confess (__PACKAGE__ . "::set() - Attempted to set an undeclared named parameter: '$keyname'\n") if (not exists $self->{-legal_parms}->{$keyname});
$self->{-parm_values}->{$keyname} = $value;
}
}
######################################################################
=over 4
=item C<clear(@parm_names);>
Clears (deletes) one or more named parameter values.
Example:
$self->clear(-this,-that,-the_other_thing);
Note: A 'cleared' value returns undef from 'get'.
=back
=cut
sub clear {
my $self = shift;
my (@parmnames) = @_;
my $parmname;
foreach $parmname (@parmnames) {
my $keyname = lc ($parmname);
confess (__PACKAGE__ . "::clear() - Attempted to clear an undeclared named parameter: '$keyname'\n") if (not exists $self->{-legal_parms}->{$keyname});
$self->{-parm_values}->{$keyname} = undef;
}
}
######################################################################
=over 4
=item C<get(@parm_names);>
Gets one or more named parameter values.
Screams and dies (well, 'confess'es) if you attempt to read
a value that has not been initialized. Results are returned
in the same order as the parameter names passed.
In a scalar context, the _last_ result is what is returned.
Example:
my ($age,$gender) = $self->get(-age,-gender);
Will 'confess' if an attempt is made to access an undeclared key or if
the requested value has not been initialized.
=back
=cut
sub get {
my $self = shift;
if ($#_ == -1) { confess(__PACKAGE__ . "::get() - Called without any parameters\n"); }
my (@results) = ();
foreach (@_) {
my $keyname = lc $_;
if (not exists $self->{-parm_values}->{$keyname}) {
confess (__PACKAGE__ . "::get() - Attempted to retrieve an undeclared or unitialized named parameter: '$keyname'\n");
}
push (@results,$self->{-parm_values}->{$keyname});
}
if (wantarray) {
return @results;
}
$results[$#results];
}
################################################################
=over 4
=item C<all_parms;>
Returns an anonymous hash containing all the currently
set keys and values. This hash is suitable for usage
with Class::NamedParms or Class::ParmList for setting
keys/values with their 'set' methods.
It works by making a shallow copy of the data. This means
that it copies the scalar values. In the case of simple
numbers and strings, this produces a new copy, in the case
of references to hashes and arrays or objects, it returns
the reference to the original object such that alterations
of the returned object are reflected in the live copy.
Example:
my $parms = $parms->all_parms;
=back
=cut
sub all_parms {
my ($self) = shift;
my (@parm_list) = $self->list_initialized_parms;
my ($all_p) = {};
foreach my $parm (@parm_list) {
$all_p->{$parm} = $self->get($parm);
}
$all_p;
}
#######################################################################
=head1 COPYRIGHT
Copyright 1999, Benjamin Franz (<URL:http://www.nihongo.org/snowhare/>) and
FreeRun Technologies, Inc. (<URL:http://www.freeruntech.com/>). All Rights Reserved.
This software may be copied or redistributed under the same terms as Perl itelf.
=head1 AUTHOR
Benjamin Franz
=head1 VERSION
1.04
=head1 TODO
Debugging.
=cut
1;
|