blib/lib/Loompa.pm

filter: Statementstmt 90.2% 93 / 103 Branchbran 90.6% 58 / 64 Conditioncond 86.0% 43 / 50 MC/DCmcdc 73.0% 19 / 26 Subroutinesub 93.3% 14 / 15 Podpod 100% 5 / 5 totaltotal 88.2% 232 / 263 CC 52 SCAR 24.7
CC
52
Cov
89%
check_methods
21.4
_make_method
13.1
__ANON__
12.0
CRAP 11.8
↑ Summary
1 package Loompa;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Loompa - Lightweight object-oriented miniature Perl assistant.
10  
11 =head1 VERSION
12  
13 Version 0.51
14  
15 =cut
16  
17 4 our $VERSION = '0.51';
18  
19 =head1 WARNING
20  
21 This code is only here because some legacy code depends on it. Do not use it
22 in new code. Use L<Moose> if you want an object/class builder.
23  
24 =head1 SYNOPSIS
25  
26 package MyCat;
27 use base qw/ Loompa /;
28  
29 sub methods {
30 [ qw/ name color temperment /]
31 }
32  
33 sub init {
34 my $self = shift;
35 $self->color( 'black' ) if $self->name eq 'Boris';
36 return $self;
37 }
38  
39 # in a nearby piece of code ...
40 use MyCat;
41  
42 my $cat = MyCat->new({
43 name => 'Boris',
44 temperment => 'evil',
45 });
46  
47 print $cat->name; # "Boris"
48 print $cat->temperment; # "evil"
49 print $cat->color; # "black"
50  
51 =head1 METHODS
52  
53 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54  
55 =head2 new([ \%properties ])
56  
57 Class method. C<\%properties> is optional. If provided, hash keys will be
58 taken as property names and hash values as property values.
59  
60 It is an error to supply a property for which you have not also created an
61 accessor method. In other words, if you do something like this:
62  
63 package Cat;
64 use base 'Loompa';
65 my $cat = Cat->new({ whiskers => 'long' });
66  
67 In this case, Loompa will C<croak> with "Method 'whiskers' not defined for
68 object."
69  
70 =cut
71  
72 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
73 # contruct a new object and take all incoming arguments as
74 # method/value pairs
75 13 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
76 13 my $class = shift;
77 13 my( $properties ) = @_;
78  
79 13 $__PACKAGE__::LOOMPA_IS_BUILDING_ME = 1;
80 13 ▶ croak 'Argument to constructor must be hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if $properties and ref $properties ne "HASH" 1 12 13
Condition100%3/3
$properties and ref $properties ne "HASH"
!ll&&!rl&&r
841
MC/DC100%2/2
$properties and ref $properties ne "HASH"
$properties ref $properties ne "HASH"
Truth table100%3/3
A and B
A= $propertiesB= ref $properties ne "HASH"
ABresult
0X0
100
111
81 if $properties and ref $properties ne 'HASH';
82  
83 12 my $self = bless {}, $class;
84 12 ▶ $self->make_methods( $self->methods )
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("methods") 4 8 12
85 if $self->can( 'methods' );
86 12 ▶ while( my( $property, $value ) = each %$properties ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$properties) 9 11 20
87 9 ▶ croak qq/Method "$property" not defined for object; caller: /
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($property) 1 8 9
88 . join ':' => caller
89 unless $self->can( $property );
90 8 $self->$property( $value );
91 }
92 11 $self->set_method_defaults;
93 11 $__PACKAGE__::LOOMPA_IS_BUILDING_ME = 0;
94  
95 11 $self->init( $properties );
96 11 $self;
97 }
98  
99 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100  
101 =head2 init()
102  
103 Blank initializer; may be overridden.
104  
105 =cut
106  
107 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
108 8 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
109 8 my $self = shift;
110 8 $self;
111 }
112  
113 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114  
115 =head2 check_methods( \@methods ) OR check_methods( \%methods )
116  
117 Class and object method; enforces API. One of C<\@methods> or C<\%methods> is
118 required. If supplied, C<\@methods> must be a list of words. If supplied,
119 C<\%methods> must be a hash reference, with keys as words and values as one of
120 the following:
121  
122 =over 4
123  
124 =item * <undef>, in which case the default getter/setter method will be used.
125  
126 =item * <scalar value>, in which case the default getter/setter method will be used, and <scalar value> used as its default value.
127  
128 =item * <coderef>, in which case <coderef> will be used instead of the default getter/setter.
129  
130 =back
131  
132 =cut
133  
134 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
135 # XXX pass { undef_ok => 1 } as $_options if you want $methods to be optional
136 41 ▶sub check_methods {
Subroutine check_methods: called CC=21 SCAR=30.6
Pod documented
137 41 my $proto = shift;
138 41 my( $methods, $_options ) = @_;
139  
140 41 ▶ if( $_options and $_options->{ undef_ok }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_options and $_options->{'undef_ok'}) { } 23 18 41
Condition66%2/3
$_options and $_options->{"undef_ok"}
!ll&&!rl&&r
18023
MC/DC50%1/2
$_options and $_options->{"undef_ok"}
$_options $_options->{"undef_ok"}
Truth table66%2/3
A and B
A= $_optionsB= $_options->{"undef_ok"}
ABresult
0X0
100
111
141 23 ▶ return unless $methods;
Branch: 1 branch
BranchTrueFalseTotal
unless $methods 1 22 23
142 }
143 else {
144 18 ▶ croak '$methods is required' unless $methods;
Branch: 1 branch
BranchTrueFalseTotal
unless $methods 1 17 18
145 }
146  
147 39 my $error = 'API error: please read the documentation for check_methods()';
148 39 ▶ if( ref $methods eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $methods eq 'ARRAY') { } 23 16 39
elsif (ref $methods eq 'HASH') { } 13 3 16
149 23 for( @$methods ) {
150 45 ▶ croak $error .' (invalid method name)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or $_ eq "" or $_ =~ /\W/ or $_ =~ /^\d/ 8 37 45
Condition88%8/9
not defined $_ or $_ eq "" or $_ =~ /\W/ or $_ =~ /^\d/
l!l&&r!l&&!r
7137
not defined $_ or $_ eq "" or $_ =~ /\W/
l!l&&r!l&&!r
3438
not defined $_ or $_ eq ""
l!l&&r!l&&!r
0342
MC/DC75%3/4
not defined $_ or $_ eq "" or $_ =~ /\W/ or $_ =~ /^\d/
not defined $_ $_ eq "" $_ =~ /\W/ $_ =~ /^\d/
Truth table80%4/5
not(A) or B or C or D
A= not defined $_B= $_ eq ""C= $_ =~ /\W/D= $_ =~ /^\d/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
151 if not defined $_
152 or $_ eq ''
153 or $_ =~ /\W/
154 or $_ =~ /^\d/; # FIXME duplicated
155 }
156 15 return scalar @$methods;
157 }
158 elsif( ref $methods eq 'HASH' ) {
159 13 ▶ croak $error .' (invalid hash reference)'
Branch: 1 branch
BranchTrueFalseTotal
unless %$methods 1 12 13
160 unless %$methods;
161 12 ▶ while( my( $key, $value ) = each %$methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$methods) 23 4 27
162 23 ▶ croak $error .' (invalid method name)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "" or $key =~ /\W/ or $key =~ /^\d/ 4 19 23
Condition66%6/9
not defined $key or $key eq "" or $key =~ /\W/ or $key =~ /^\d/
l!l&&r!l&&!r
3119
not defined $key or $key eq "" or $key =~ /\W/
l!l&&r!l&&!r
0320
not defined $key or $key eq ""
l!l&&r!l&&!r
0023
MC/DC50%2/4
not defined $key or $key eq "" or $key =~ /\W/ or $key =~ /^\d/
not defined $key $key eq "" $key =~ /\W/ $key =~ /^\d/
Truth table60%3/5
not(A) or B or C or D
A= not defined $keyB= $key eq ""C= $key =~ /\W/D= $key =~ /^\d/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
163 if not defined $key
164 or $key eq ''
165 or $key =~ /\W/
166 or $key =~ /^\d/; # FIXME duplicated
167 19 ▶ croak $error .' (invalid hash reference)'
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ref $value and ref $value ne "CODE" 4 15 19
Condition100%6/6
defined $value and ref $value and ref $value ne "CODE"
!ll&&!rl&&r
1054
defined $value and ref $value
!ll&&!rl&&r
559
MC/DC100%3/3
defined $value and ref $value and ref $value ne "CODE"
defined $value ref $value ref $value ne "CODE"
Truth table100%4/4
A and B and C
A= defined $valueB= ref $valueC= ref $value ne "CODE"
ABCresult
0XX0
10X0
1100
1111
168 if defined $value and ref $value and ref $value ne 'CODE';
169 }
170 4 return scalar keys %$methods;
171 }
172 else {
173 3 croak $error .' (invalid data type: argument to make_methods() must be arrayref or hashref)'
174 }
175 0 die 'Should never get here';
176 }
177  
178 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179  
180 =head2 make_methods( \@methods, [ $subref ], \%options ) OR make_methods( \%methods, undef, \%options )
181  
182 Class and object method. Makes methods for items in C<$methods>.
183  
184 If <$methods> is an array reference, one method will be created for each name
185 in the list. If supplied, C<$subref> must be a subroutine reference, and will
186 be used in place of the standard setter/getter.
187  
188 If <%options> is provided, these are legal values:
189  
190 =over 4
191  
192 =item override_existing
193  
194 Loompa's default behavior is to create methods only once, thereafter returning
195 before the construction step. If you set C<override_existing> to true, each
196 method provided will be constructed anew.
197  
198 =item object
199  
200 Loompa's default behavior is to pass the object in method-call style.
201  
202 This is probably undesired behavior in a base class that defines many custom
203 class methods. Setting the 'object' value to a package name will override the
204 default passed-in object and provide that instead. Note that this is done
205 through a closure and may not be reasonable on your memory usage if you want to
206 define lots of methods.
207  
208 You can also do dirtier things with this option, but I'm going to refrain from
209 describing them.
210  
211 =back
212  
213 if C<$methods> is a hash reference, the key/value pair will be understood as
214 C<$method_name> and C<$method_subroutine>. For example:
215  
216 CatClass->make_methods({
217 boris => undef,
218 sasha => $method_cat,
219 shaolin => $method_fat_cat,
220 });
221 my $cat = CatClass->new;
222 $cat->boris; # calls default setter/getter
223 $cat->sasha; # calls the method referenced by C<$method_cat>
224 $cat->shaolin; # calls the method referenced by C<$method_fat_cat>
225  
226 In this case,
227  
228 PLEASE NOTE that the second argument to your custom subroutine will be the name
229 of the subroutine as it was called. In other words, you should write something
230 like this:
231  
232 package MyClass;
233 use base qw/ Loompa /;
234  
235 my $color_method = sub {
236 my $self = shift;
237 my( $name, $emotion ) = @_;
238 return "My name is '$name' and I am '$emotion.'";
239 };
240 MyClass->make_methods( 'orange', 'brown', $color_method );
241  
242 MyClass->orange( 'happy' ); ## "My name is 'orange' and I am 'happy.'"
243 MyClass->brown( 'sad' ); ## "My name is 'brown' and I am 'sad.'"
244  
245 =cut
246  
247 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
248 24 ▶sub make_methods {
Subroutine make_methods: called CC=6 SCAR=17.9
Pod documented
249 24 my $proto = shift;
250 24 my( $methods, $subref, $options ) = @_;
251  
252 24 ▶ return unless $methods; # XXX does this make undef_ok obsolete?
Branch: 1 branch
BranchTrueFalseTotal
unless $methods 2 22 24
253 22 $proto->check_methods( $methods, { undef_ok => 1 });
254 15 ▶ if( ref $methods eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $methods eq 'ARRAY') { } 13 2 15
elsif (ref $methods eq 'HASH') { } 2 0 2
255 $proto->_make_method( $_, $subref, $options )
256 13 for @$methods;
257 }
258 elsif( ref $methods eq 'HASH' ) {
259 2 ▶ while( my( $property, $prototype ) = each %$methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$methods) 9 2 11
260 9 $proto->_make_method( $property, $prototype, $options );
261 }
262 }
263 15 return $proto;
264 }
265  
266 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
267 # create an accessor method $field in the calling package
268 # Creates a getter/setter method for C<$name>. If supplied, C<$subref> must be a
269 # subroutine reference, and will be used in place of the standard setter/getter.
270 44 ▶sub _make_method {
Subroutine _make_method: called CC=12 SCAR=25.7
271 44 my $proto = shift;
272 44 my( $name, $prototype, $options ) = @_;
273  
274 44 ▶ $options ||= {}; # makes it easier to evaluate later
Condition100%2/2
$options ||= {}
l!l
737
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
275 44 ▶ croak 'Second argument, if supplied, must be scalar value or subroutine reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $prototype and (ref $prototype and not ref $prototype eq "CODE") 2 42 44
Condition100%6/6
defined $prototype and (ref $prototype and not ref $prototype eq "CODE")
!ll&&!rl&&r
21212
ref $prototype and not ref $prototype eq "CODE"
!ll&&!rl&&r
4172
MC/DC100%3/3
defined $prototype and (ref $prototype and not ref $prototype eq "CODE")
defined $prototype ref $prototype not ref $prototype eq "CODE"
Truth table100%4/4
A and B and not(C)
A= defined $prototypeB= ref $prototypeC= not ref $prototype eq "CODE"
ABCresult
0XX0
10X0
1100
1111
276 if defined $prototype and ( ref $prototype and not ref $prototype eq 'CODE' );
277  
278 42 my( $default, $subref );
279 42 ▶ if( ref $prototype eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prototype eq "CODE") 17 25 42
280 17 $subref = $prototype;
281 }
282  
283 42 ▶ my $package = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
31110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
284 42 ▶ return if defined &{ $package ."::$name" } and not $options->{ override_existing };
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$package . "::$name";} and not $options->{"override_existing"} 9 33 42
Condition100%3/3
defined &{$package . "::$name";} and not $options->{"override_existing"}
!ll&&!rl&&r
3219
MC/DC100%2/2
defined &{$package . "::$name";} and not $options->{"override_existing"}
defined &{$package . "::$name";} not $options->{"override_existing"}
Truth table100%3/3
A and not(B)
A= defined &{$package . "::$name";}B= not $options->{"override_existing"}
ABresult
0X0
100
111
285  
286 4 ▶ no warnings qw/ redefine /;
Subroutine BEGIN: called CC=1 SCAR=0.0
287 4 ▶ no strict qw/ refs /;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 33 ▶ if( $subref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subref) { } 17 16 33
289 17 ▶ if ($options->{object}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'object'}) { } 6 11 17
290 6 my $object = $options->{object};
291 6 ▶ *{ $package ."::$name" } = sub { shift; $subref->( $object, $name, @_ )};
Subroutine __ANON__: called CC=1 SCAR=0.0
292 } else {
293 11 ▶ *{ $package ."::$name" } = sub { $subref->( shift, $name, @_ )};
Subroutine __ANON__: called CC=1 SCAR=0.0
294 }
295 }
296 else {
297 16 ▶ if ($options->{object}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'object'}) { } 0 16 16
298 0 my $object = $options->{object};
299 0 ▶ *{ $package ."::$name" } = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
300 0 shift;
301 0 my $self = $object;
302  
303 0 ▶ return $self->{ $name } unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
304 0 $self->{ $name } = shift;
305 0 ▶ croak 'Please pass only one value'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
306 if @_;
307 0 $self->{ $name };
308 0 };
309 } else {
310 16 ▶ *{ $package ."::$name" } = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
311 60 my $self = shift;
312  
313 60 ▶ return $self->{ $name } unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 34 26 60
314 26 $self->{ $name } = shift;
315 26 ▶ croak 'Please pass only one value'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 25 26
316 if @_;
317 25 $self->{ $name };
318 16 };
319 }
320 }
321 }
322  
323 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
324  
325 =head2 set_method_defaults()
326  
327 Object method; sets default values for accessors, as defined by local
328 C<methods> method.
329  
330 =cut
331  
332 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
333 13 ▶sub set_method_defaults {
Subroutine set_method_defaults: called CC=7 SCAR=19.5
Pod documented
334 13 my $self = shift;
335  
336 return
337 13 ▶ unless $self->can( 'methods' ) and ref $self->methods and ref $self->methods eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("methods") and ref $self->methods and ref $self->methods eq "HASH" 11 2 13
Condition83%5/6
$self->can("methods") and ref $self->methods and ref $self->methods eq "HASH"
!ll&&!rl&&r
832
$self->can("methods") and ref $self->methods
!ll&&!rl&&r
805
MC/DC66%2/3
$self->can("methods") and ref $self->methods and ref $self->methods eq "HASH"
$self->can("methods") ref $self->methods ref $self->methods eq "HASH"
Truth table75%3/4
A and B and C
A= $self->can("methods")B= ref $self->methodsC= ref $self->methods eq "HASH"
ABCresult
0XX0
10X0
1100
1111
338  
339 2 my %properties = %{ $self->methods };
340 2 ▶ while( my( $property, $value ) = each %properties ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %properties) 10 2 12
341 10 ▶ next if defined $self->$property;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$property 7 3 10
342 3 ▶ $self->$property( $value )
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 2 3
343 if defined $value;
344 }
345 2 $self;
346 }
347  
348  
349 4 'loompa';
350  
351 __END__
↑ Summary