Class/Fields.pm

filter: Statementstmt 73.3% 66 / 90 Branchbran 65.3% 17 / 26 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 92.8% 13 / 14 Podpod 100% 5 / 5 totaltotal 62.9% 107 / 170 CC 23 SCAR 32.2
CC
23
Cov
65%
dump_all_attribs
90.0
show_fields
6.4
field_attribs
4.0
CRAP 25.0
↑ Summary Class/Fields/Fuxor.pm »
1 # $Id$
2  
3 package Class::Fields;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use vars qw(@ISA @EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 require Exporter;
10 3 @ISA = qw(Exporter);
11  
12 # is_* will push themselves onto @EXPORT
13 3 @EXPORT = qw( field_attrib_mask
14 field_attribs
15 dump_all_attribs
16 show_fields
17 is_public
18 is_private
19 is_protected
20 is_inherited
21 is_field
22 );
23  
24 3 $VERSION = '0.204';
25  
26 3 ▶use Class::Fields::Fuxor;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Class::Fields::Attribs;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Mapping of attribute names to their internal values.
30 3 ▶use vars qw(%NAMED_ATTRIBS);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 %NAMED_ATTRIBS = (
33 Public => PUBLIC,
34 Private => PRIVATE,
35 Inherited => INHERITED,
36 Protected => PROTECTED,
37 );
38 }
39  
40 =pod
41  
42 =head1 NAME
43  
44 Class::Fields - Inspect the fields of a class.
45  
46  
47 =head1 SYNOPSIS
48  
49 use Class::Fields;
50  
51 is_field ($class, $field);
52 is_public ($class, $field);
53 is_private ($class, $field);
54 is_protected($class, $field);
55 is_inherited($class, $field);
56  
57 @fields = show_fields($class, @attribs);
58  
59 $attrib = field_attrib_mask($class, $field);
60 @attribs = field_attribs($class, $field);
61  
62 dump_all_attribs(@classes);
63  
64  
65 # All functions also work as methods.
66 package Foo;
67 use base qw( Class::Fields );
68  
69 Foo->is_public($field);
70 @fields = Foo->show_fields(@attribs);
71 # ...etc...
72  
73  
74 =head1 DESCRIPTION
75  
76 B<NOTE> This module, and the fields system, is largely obsolete.
77 Please consider using one of the many accessor generating modules, or
78 just skip directly to a complete object oriented system like L<Moose>
79 or L<Mouse>.
80  
81 A collection of utility functions/methods for examining the data
82 members of a class. It provides a nice, high-level interface that
83 should stand the test of time and Perl upgrades nicely.
84  
85 The functions in this module also serve double-duty as methods and can
86 be used that way by having your module inherit from it. For example:
87  
88 package Foo;
89 use base qw( Class::Fields );
90 use fields qw( this that _whatever );
91  
92 print "'_whatever' is a private data member of 'Foo'" if
93 Foo->is_private('_whatever');
94  
95 # Let's assume we have a new() method defined for Foo, okay?
96 $obj = Foo->new;
97 print "'this' is a public data member of 'Foo'" if
98 $obj->is_public('this');
99  
100 =over 4
101  
102 =item B<is_field>
103  
104 is_field($class, $field);
105 $class->is_field($field);
106  
107 Simply asks if a given $class has the given $field defined in it.
108  
109 =cut
110  
111 2 ▶sub is_field {
Subroutine is_field: called CC=3 SCAR=11.4
Pod documented
112 2 my($proto, $field) = @_;
113  
114 2 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
115 2 ▶ return defined field_attrib_mask($class, $field) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined field_attrib_mask($class, $field) ? : 1 1 2
116 }
117  
118 =pod
119  
120 =item B<is_public>
121  
122 =item B<is_private>
123  
124 =item B<is_protected>
125  
126 =item B<is_inherited>
127  
128 is_public($class, $field);
129 is_private($class, $field);
130 ...etc...
131 or
132 $obj->is_public($field);
133 or
134 Class->is_public($field);
135  
136 A bunch of functions to quickly check if a given $field in a given $class
137 is of a given type. For example...
138  
139 package Foo;
140 use public qw( Ford );
141 use private qw( _Nixon );
142  
143 package Bar;
144 use base qw(Foo);
145  
146 # This will print only 'Ford is public' because Ford is a public
147 # field of the class Bar. _Nixon is a private field of the class
148 # Foo, but it is not inherited.
149 print 'Ford is public' if is_public('Bar', 'Ford');
150 print '_Nixon is inherited' if is_inherited('Foo', '_Nixon');
151  
152  
153 =cut
154  
155 # Generate is_public, etc... from %NAMED_ATTRIBS For each attribute we
156 # generate a simple named closure. Seemed the laziest way to do it,
157 # lets us update %NAMED_ATTRIBS without having to make a new function.
158 3 ▶while ( my($attrib, $attr_val) = each %NAMED_ATTRIBS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Class::Fields::NAMED_ATTRIBS) 12 3 15
159 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 12 my $fname = 'is_'.lc $attrib;
161 12 ▶ *{$fname} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
162 13 my($proto, $field) = @_;
163
164 # So we can be called either as a function or a method from
165 # a class name or an object.
166 13 ▶ my($class) = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
2110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
167 13 my $fattrib = field_attrib_mask($class, $field);
168
169 13 ▶ return unless defined $fattrib;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fattrib 2 11 13
170
171 11 return $fattrib & $attr_val;
172 12 };
173
174 12 push @EXPORT, $fname;
175 }
176  
177  
178 =pod
179  
180 =item B<show_fields>
181  
182 @all_fields = show_fields($class);
183 @fields = show_fields($class, @attribs);
184 or
185 @all_fields = $obj->show_fields;
186 @fields = $obj->show_fields(@attribs);
187 or
188 @all_fields = Class->show_fields;
189 @fields = Class->show_fields(@attribs);
190  
191 This will list all fields in a given $class that have the given set of
192 @attribs. If @attribs is not given it will simply list all fields.
193  
194 The currently available attributes are:
195 Public, Private, Protected and Inherited
196  
197 For example:
198  
199 package Foo;
200 use fields qw(this that meme);
201  
202 package Bar;
203 use Class::Fields;
204 use base qw(Foo);
205 use fields qw(salmon);
206  
207 # @fields contains 'this', 'that' and 'meme' since they are Public and
208 # Inherited. It doesn't contain 'salmon' since while it is
209 # Public it is not Inherited.
210 @fields = show_fields('Bar', qw(Public Inherited));
211  
212 =cut
213  
214 9 ▶sub show_fields {
Subroutine show_fields: called CC=6 SCAR=18.5
Pod documented
215 9 my($proto, @attribs) = @_;
216  
217 # Allow its tri-nature.
218 9 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
219  
220 9 ▶ return unless has_fields($class);
Branch: 1 branch
BranchTrueFalseTotal
unless has_fields($class) 1 8 9
221  
222 8 my $fields = get_fields($class);
223  
224 # Shortcut: Return all fields if they don't specify a set of
225 # attributes.
226 8 ▶ return keys %$fields unless @attribs;
Branch: 1 branch
BranchTrueFalseTotal
unless @attribs 1 7 8
227
228 # Figure out the bitmask for the attribute set they'd like.
229 7 my $want_attr = 0;
230 7 foreach my $attrib (@attribs) {
231 9 ▶ unless( defined $NAMED_ATTRIBS{$attrib} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Class::Fields::NAMED_ATTRIBS{$attrib}) 0 9 9
232 0 require Carp;
233 0 Carp::croak("'$attrib' is not a valid field attribute");
234 }
235 9 $want_attr |= $NAMED_ATTRIBS{$attrib};
236 }
237  
238 # Return all fields with the requested bitmask.
239 7 my $fattr = get_attr($class);
240 7 return grep { ($fattr->[$fields->{$_}] & $want_attr) == $want_attr}
241 keys %$fields;
242 }
243  
244 =pod
245  
246 =item B<field_attrib_mask>
247  
248 $attrib = field_attrib_mask($class, $field);
249 or
250 $attrib = $obj->field_attrib_mask($field);
251 or
252 $attrib = Class->field_attrib_mask($field);
253  
254 It will tell you the numeric attribute for the given $field in the
255 given $class. $attrib is a bitmask which must be interpreted with
256 the PUBLIC, PRIVATE, etc... constants from Class::Fields::Attrib.
257  
258 field_attribs() is probably easier to work with in general.
259  
260 =cut
261  
262 17 ▶sub field_attrib_mask {
Subroutine field_attrib_mask: called CC=3 SCAR=11.2
Pod documented
263 17 my($proto, $field) = @_;
264 17 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0170
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
265 17 my $fields = get_fields($class);
266 17 my $fattr = get_attr($class);
267 17 ▶ return unless defined $fields->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields->{$field} 3 14 17
268 14 return $fattr->[$fields->{$field}];
269 }
270  
271 =pod
272  
273 =item B<field_attribs>
274  
275 @attribs = field_attribs($class, $field);
276 or
277 @attribs = $obj->field_attribs($field);
278 or
279 @attribs = Class->field_attribs($field);
280  
281 Exactly the same as field_attrib_mask(), except that instead of
282 returning a bitmask it returns a somewhat friendlier list of
283 attributes which are applied to this field. For example...
284  
285 package Foo;
286 use fields qw( yarrow );
287  
288 package Bar;
289 use base qw(Foo);
290  
291 # @attribs will contain 'Public' and 'Inherited'
292 @attribs = field_attribs('Bar', 'yarrow');
293  
294 The attributes returned are the same as those taken by show_fields().
295  
296 =cut
297  
298 1 ▶sub field_attribs {
Subroutine field_attribs: called CC=4 SCAR=14.0
Pod documented
299 1 my($proto, $field) = @_;
300 1 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
301  
302 1 my @attribs = ();
303 1 my $attr_mask = field_attrib_mask($class, $field);
304
305 1 ▶ while( my($attr_name, $attr_val) = each %NAMED_ATTRIBS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Class::Fields::NAMED_ATTRIBS) 4 1 5
306 4 ▶ push @attribs, $attr_name if $attr_mask & $attr_val;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_mask & $attr_val 2 2 4
307 }
308  
309 1 return @attribs;
310 }
311  
312 =pod
313  
314 =item B<dump_all_attribs>
315  
316 dump_all_attribs;
317 dump_all_attribs(@classes);
318 or
319 Class->dump_all_attribs;
320 or
321 $obj->dump_all_attribs;
322  
323 A debugging tool which simply prints to STDERR everything it can about
324 a given set of @classes in a relatively formated manner.
325  
326 Alas, this function works slightly differently if used as a function
327 as opposed to a method:
328  
329 When called as a function it will print out attribute information
330 about all @classes given. If no @classes are given it will print out
331 the attributes of -every- class it can find that has attributes.
332  
333 When uses as a method, it will print out attribute information for the
334 class or object which uses the method. No arguments are accepted.
335  
336 I'm not entirely happy about this split and I might change it in the
337 future.
338  
339 =cut
340  
341 # Backwards compatiblity.
342 3 *_dump = \&dump_all_attribs;
343  
344 #'#
345 0 ▶sub dump_all_attribs {
Subroutine dump_all_attribs: not called CC=9 SCAR=45.0
Pod documented
346 0 my @classes = @_;
347  
348 # Everything goes to STDERR.
349 0 my $old_fh = select(STDERR);
350  
351 # Disallow $obj->dump_all_attribs(@classes); Too ambiguous to live.
352 # Alas, I can't check for Class->dump_all_attribs(@classes).
353 0 ▶ if ( @classes > 1 and ref $classes[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@classes > 1 and ref $classes[0]) 0 0 0
Condition0%0/3
@classes > 1 and ref $classes[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@classes > 1 and ref $classes[0]
@classes > 1 ref $classes[0]
Truth table0%0/3
A and B
A= @classes > 1B= ref $classes[0]
ABresult
0X0
100
111
354 0 require Carp;
355 0 Carp::croak('$obj->dump_all_attribs(@classes) is too ambiguous.'.
356 'Use only as $obj->dump_all_attribs()');
357 }
358  
359 # Allow $obj->dump_all_attribs; to work.
360 0 ▶ $classes[0] = ref $classes[0] || $classes[0] if @classes == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @classes == 1 0 0 0
Condition0%0/3
ref $classes[0] || $classes[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $classes[0] || $classes[0]
ref $classes[0] $classes[0]
Truth table0%0/3
A || B
A= ref $classes[0]B= $classes[0]
ABresult
1X1
011
000
361  
362 # Have to do a little encapsulation breaking here. Oh well, at least
363 # its keeping it in the family.
364 0 ▶ @classes = sort keys %fields::attr unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 0 0 0
365  
366 0 for my $class (@classes) {
367 0 print "\n$class";
368 0 ▶ if (@{"$class\::ISA"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{"${class}::ISA";}) 0 0 0
369 0 print " (", join(", ", @{"$class\::ISA"}), ")";
370 }
371 0 print "\n";
372 0 my $fields = get_fields($class);
373 0 for my $f (sort {$fields->{$a} <=> $fields->{$b}} keys %$fields) {
374 0 my $no = $fields->{$f};
375 0 print " $no: $f";
376 0 print "\t(", join(", ", field_attribs($class, $f)), ")";
377 0 print "\n";
378 }
379 }
380
381 0 select($old_fh);
382 }
383  
384 =pod
385  
386 =head1 EXAMPLES
387  
388 Neat tricks that can be done with this module:
389  
390 =over 4
391  
392 =item An integrity check for your object.
393  
394 Upon destruction, check to make sure no strange keys were added to
395 your object hash. This is a nice check against typos and other
396 modules sticking their dirty little fingers where they shouldn't be
397 if you're not using a pseudo-hash.
398  
399 sub DESTROY {
400 my($self) = @_;
401 my($class) = ref $self;
402  
403 my %fields = map { ($_,1) } $self->show_fields;
404 foreach my $key ( keys %$self ) {
405 warn "Strange key '$key' found in object '$self' ".
406 "of class '$class'" unless
407 exists $fields{$key};
408 }
409 }
410  
411 =item Autoloaded accessors for public data members.
412  
413 Proper OO dogma tells you to do all public data access through
414 accessors (methods who's sole purpose is to get and set data in your
415 object). This can be a royal pain in the ass to write and can also
416 get rapidly unmaintainable since you wind up with a series of nearly
417 identical methods.
418  
419 *Perfect* for an autoloader!
420  
421 package Test::Autoload::Example;
422 use base qw(Class::Fields);
423 use public qw(this that up down);
424 use private qw(_left _right);
425  
426 sub AUTOLOAD {
427 my $self = $_[0];
428 my $class = ref $self;
429  
430 my($field) = $AUTOLOAD =~ /::([^:]+)$/;
431  
432 return if $field eq 'DESTROY';
433  
434 # If its a public field, set up a named closure as its
435 # data accessor.
436 if ( $self->is_public($field) ) {
437 *{$class."::$field"} = sub {
438 my($self) = shift;
439 if (@_) {
440 $self->{$field} = shift;
441 }
442 return $self->{$field};
443 };
444 goto &{$class."::$field"};
445 } else {
446 die "'$field' is not a public data member of '$class'";
447 }
448 }
449  
450 L<Class::Accessor/EXAMPLES> for a much simpler version of this same
451 technique.
452  
453 =back
454  
455 =head1 COPYRIGHT AND LICENSE
456  
457 Copyright 2001-2011 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
458  
459 This program is free software; you can redistribute it and/or
460 modify it under the same terms as Perl itself.
461  
462 See L<http://dev.perl.org/licenses/artistic.html>
463  
464  
465 =head1 AUTHOR
466  
467 Michael G Schwern <schwern@pobox.com> with much code liberated from the
468 original fields.pm.
469  
470  
471 =head1 THANKS
472  
473 Thanks to Tels for his big feature request/bug report.
474  
475  
476 =head1 SEE ALSO
477  
478 This module and the L<fields> system are obsolete.
479 L<Moose>, L<Mouse>, L<Class::Accessor> are better alternatives.
480  
481 L<fields>, L<public>, L<private>, L<protected>
482  
483 Modules with similar effects... L<Tie::SecureHash>
484  
485 =cut
486  
487 3 return q|I'll get you next time, Gadget!|;
↑ Summary Class/Fields/Fuxor.pm »