blib/lib/Object/ArrayType/New.pm

filter: Statementstmt 87.2% 89 / 102 Branchbran 58.0% 36 / 62 Conditioncond 33.3% 12 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 64.0% 153 / 239 CC 44 SCAR 22.2
CC
44
Cov
68%
new
15.6
new
15.6
new
12.8
CRAP 9.2
↑ Summary
1 package Object::ArrayType::New;
2 2 $Object::ArrayType::New::VERSION = '1.001001';
3 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util 'blessed', 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶sub import {
Subroutine import: called CC=5 SCAR=16.5
10 5 my ($class, $params) = @_;
11 5 ▶ $params = [] unless defined $params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 1 4 5
12 5 ▶ croak "Expected an ARRAY or HASH but got $params"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params and builtin::reftype($params) eq "ARRAY" or builtin::reftype($params) eq "HASH" 0 5 5
Condition66%4/6
ref $params and builtin::reftype($params) eq "ARRAY" or builtin::reftype($params) eq "HASH"
l!l&&r!l&&!r
410
ref $params and builtin::reftype($params) eq "ARRAY"
!ll&&!rl&&r
014
MC/DC0%0/3
ref $params and builtin::reftype($params) eq "ARRAY" or builtin::reftype($params) eq "HASH"
ref $params builtin::reftype($params) eq "ARRAY" builtin::reftype($params) eq "HASH"
Truth table40%2/5
A and B or C
A= ref $paramsB= builtin::reftype($params) eq "ARRAY"C= builtin::reftype($params) eq "HASH"
ABCresult
11X1
0X11
1011
0X00
1000
13 unless ref $params
14 and reftype $params eq 'ARRAY'
15 or reftype $params eq 'HASH';
16  
17 5 my $target = caller;
18 5 $class->_validate_and_install($target => $params)
19 }
20  
21 15 ▶sub _inject_code {
Subroutine _inject_code: called CC=6 SCAR=22.8
22 15 my ($class, $target, $code) = @_;
23 15 ▶ confess "Expected a target package and string to inject"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and defined $code 0 15 15
Condition33%1/3
defined $target and defined $code
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $target and defined $code
defined $target defined $code
Truth table33%1/3
A and B
A= defined $targetB= defined $code
ABresult
0X0
100
111
24 unless defined $target and defined $code;
25 15 my $run = "package $target; $code; 1;";
26 15 ▶ warn "(eval ->) $run\n" if $ENV{OBJECT_ARRAYTYPE_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"OBJECT_ARRAYTYPE_DEBUG"} 0 15 15
27 15 local $@;
28 0 ▶ eval $run and not $@ or confess "eval: $@";
Subroutine new: called CC=8 SCAR=27.4
Subroutine new: called CC=8 SCAR=27.4
Subroutine new: called CC=7 SCAR=25.5
Subroutine new: called CC=6 SCAR=23.3
Branch: 19 branches
BranchTrueFalseTotal
eval $run and not $@ or confess("eval: $@") 15 0 15
if (@_ == 1) { } 1 2 3
elsif (@_ % 2) { } 0 2 2
unless ref $_[0] and builtin::reftype($_[0]) eq "HASH" 0 1 1
defined $args{'foo'} ? : 2 1 3
defined $args{'bar'} ? : 1 2 3
defined $args{'baz'} ? : 0 2 2
if (@_ == 1) { } 0 1 1
elsif (@_ % 2) { } 0 1 1
unless ref $_[0] and builtin::reftype($_[0]) eq "HASH" 0 0 0
defined $args{'bar'} ? : 1 0 1
defined $args{'foo'} ? : 1 0 1
if (@_ == 1) { } 0 1 1
elsif (@_ % 2) { } 0 1 1
unless ref $_[0] and builtin::reftype($_[0]) eq "HASH" 0 0 0
defined $args{'bar'} ? : 1 0 1
if (@_ == 1) { } 0 1 1
elsif (@_ % 2) { } 0 1 1
unless ref $_[0] and builtin::reftype($_[0]) eq "HASH" 0 0 0
Condition25%7/27
eval $run and not $@
!ll&&!rl&&r
0015
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
001
builtin::blessed($class) || $class
l!l&&r!l&&!r
120
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
000
builtin::blessed($class) || $class
l!l&&r!l&&!r
010
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
000
builtin::blessed($class) || $class
l!l&&r!l&&!r
010
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
000
builtin::blessed($class) || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
eval $run and not $@
eval $run not $@
MC/DC0%0/2
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
ref $_[0] builtin::reftype($_[0]) eq "HASH"
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class) $class
MC/DC0%0/2
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
ref $_[0] builtin::reftype($_[0]) eq "HASH"
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class) $class
MC/DC0%0/2
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
ref $_[0] builtin::reftype($_[0]) eq "HASH"
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class) $class
MC/DC0%0/2
ref $_[0] and builtin::reftype($_[0]) eq "HASH"
ref $_[0] builtin::reftype($_[0]) eq "HASH"
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class) $class
Truth table33%1/3
A and not(B)
A= eval $runB= not $@
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= ref $_[0]B= builtin::reftype($_[0]) eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= ref $_[0]B= builtin::reftype($_[0]) eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= ref $_[0]B= builtin::reftype($_[0]) eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= ref $_[0]B= builtin::reftype($_[0]) eq "HASH"
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
29 15 1
30 }
31  
32 10 ▶sub _inject_constant {
Subroutine _inject_constant: called CC=2 SCAR=7.0
33 10 my ($class, $target, $name, $val) = @_;
34 10 ▶ my $code = ref $val ? "sub $name () { \$val }"
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 10 10
35 10 : "sub $name () { ${\ B::perlstring($val) } }";
36 10 $class->_inject_code($target => $code)
37 }
38  
39 5 ▶sub _install_constants {
Subroutine _install_constants: called CC=2 SCAR=6.9
40 5 my ($class, $target, $items) = @_;
41 5 my $idx = 0;
42 5 for my $item (@$items) {
43 10 my $constant = $item->{constant};
44 10 $class->_inject_constant($target => $constant => $idx++);
45 }
46 1
47 5 }
48  
49 5 ▶sub _validate_and_install {
Subroutine _validate_and_install: called CC=6 SCAR=18.0
50 5 my ($class, $target, $params) = @_;
51 5 ▶ my @items = reftype $params eq 'HASH' ? %$params : @$params;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($params) eq 'HASH' ? : 1 4 5
52  
53 5 my @install;
54 5 ▶ PARAM: while (my ($initarg, $def) = splice @items, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@items, 0, 2)) 10 5 15
55 10 ▶ $initarg = '' unless defined $initarg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $initarg 0 10 10
56 10 ▶ my $store = $def ? $def : uc $initarg;
Branch: 1 branch
BranchTrueFalseTotal
$def ? : 5 5 10
57 10 ▶ confess "No init arg and no constant specified!" unless $store;
Branch: 1 branch
BranchTrueFalseTotal
unless $store 0 10 10
58 10 push @install, +{
59 name => $initarg,
60 constant => $store,
61 };
62 }
63  
64 5 $class->_install_constants($target => \@install);
65 5 $class->_install_constructor($target => \@install);
66 }
67  
68 5 ▶sub _generate_storage {
Subroutine _generate_storage: called CC=3 SCAR=11.0
69 5 my (undef, undef, $items) = @_;
70 5 my $code = " my \$self = bless [\n";
71 5 for my $item (@$items) {
72 10 my $attr = $item->{name};
73 10 ▶ $code .= $attr ?
Branch: 1 branch
BranchTrueFalseTotal
$attr ? : 8 2 10
74 qq[ (defined \$args{$attr} ? \$args{$attr} : undef),\n]
75 : qq[ undef,\n]
76 }
77 5 $code .= ' ], (Scalar::Util::blessed($class) || $class);';
78 5 $code
79 }
80  
81 5 ▶sub _install_constructor {
Subroutine _install_constructor: called CC=1 SCAR=0.0
82 5 my ($class, $target, $items) = @_;
83  
84 5 my $code = <<'_EOC';
85 sub new {
86 my $class = shift; my %args;
87 if (@_ == 1) {
88 Carp::confess "Expected single param to be a HASH but got $_[0]"
89 unless ref $_[0] and Scalar::Util::reftype $_[0] eq 'HASH';
90 %args = %{ $_[0] }
91 } elsif (@_ % 2) {
92 Carp::confess "Expected either a HASH or a list of key/value pairs"
93 } else {
94 %args = @_
95 }
96  
97 _EOC
98
99 5 $code .= $class->_generate_storage($target => $items);
100 5 $code .= "\n \$self\n}\n";
101 5 $class->_inject_code($target => $code)
102 }
103  
104 2 ▶print
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 2 2
105 q[<SpiceMan> also every time you @result = `curl blahblah`],
106 qq[ LeoNerd uses passive voice\n]
107 unless caller;
108 2 1;
109  
110 =pod
111  
112 =for Pod::Coverage import
113  
114 =head1 NAME
115  
116 Object::ArrayType::New - Inject constants and constructor for ARRAY-type objects
117  
118 =head1 SYNOPSIS
119  
120 package MyObject;
121 use strict; use warnings;
122 use Object::ArrayType::New
123 [ foo => 'FOO', bar => 'BAR' ];
124 sub foo { shift->[FOO] }
125 sub bar { shift->[BAR] ||= [] }
126  
127 package main;
128 my $obj = MyObject->new(foo => 'baz');
129 my $foo = $obj->foo; # baz
130 my $bar = $obj->bar; # []
131  
132 =head1 DESCRIPTION
133  
134 ARRAY-backed objects are light and fast, but obviously slightly more
135 complicated to cope with than just stuffing key/value pairs into a HASH.
136 The easiest way to keep track of where things live is to set up some named
137 constants to index into the ARRAY -- you can access your indexes by name,
138 and gain compile-time typo checking as an added bonus.
139  
140 A common thing I find myself doing looks something like:
141  
142 package MySimpleObject;
143 use strict; use warnings;
144  
145 sub TAG () { 0 }
146 sub BUF () { 1 }
147 # ...
148  
149 sub new {
150 my $class = shift;
151 my %params = @_ > 1 ? @_ : %{ $_[0] };
152 bless [
153 $params{tag}, # TAG
154 ($params{buffer} || []) # BUF
155 # ...
156 ], $class
157 }
158 sub tag { shift->[TAG] }
159 sub buffer { shift->[BUF] }
160 # ...
161  
162 ... when I'd rather be doing something more like the L</SYNOPSIS>.
163  
164 This tiny module takes, as arguments to C<import>, an ARRAY of pairs mapping a
165 C<new()> parameter name to the name of a constant. The constant represents the
166 item's position in the object's backing ARRAY.
167  
168 If the B<constant>'s name is boolean false, the uppercased parameter name is
169 used as the name of the constant:
170  
171 use Object::ArrayType::New
172 [ foo => '', bar => '' ];
173 # same as foo => 'FOO', bar => 'BAR'
174  
175 If the B<parameter>'s name is boolean false, there is no construction-time
176 parameter. The constant is installed and the appropriate position in the
177 backing ARRAY is set to C<undef> at construction time; this can be useful for
178 private attributes:
179  
180 use Object::ArrayType::New
181 [ foo => 'FOO', '' => 'BAR' ];
182 sub foo { shift->[FOO] ||= 'foo' }
183 sub _bar { shift->[BAR] ||= [] }
184  
185 An appropriate constructor is generated and installed, as well as constants
186 that can be used within the class to index into the C<$self> object.
187  
188 The generated constructor takes parameters as either a list of pairs or a
189 single HASH. Parameters not specified at construction time are C<undef>.
190  
191 That's it; no accessors, no defaults, no type-checks, no required attributes,
192 nothing fancy. L<Class::Method::Modifiers> may be convenient there; the above
193 raw Perl example could be written something like:
194  
195 use Object::ArrayType::New [ tag => '', buffer => 'BUF' ];
196 sub tag { shift->[TAG] }
197 sub buffer { shift->[BUF] }
198 use Class::Method::Modifers;
199 around new => sub {
200 my ($orig, $class) = splice @_, 0, 2;
201 my $self = $class->$orig(@_);
202 $self->[BUF] = [] unless defined $self->[BUF];
203 $self
204 };
205  
206 if C<< $ENV{OBJECT_ARRAYTYPE_DEBUG} >> is true, generated code is printed to
207 STDERR before being evaluated.
208  
209 Constants aren't currently sanity-checked ahead of time; attempting to use
210 invalid identifiers will result in vague 'Illegal declaration ...' failures.
211  
212 =head1 AUTHOR
213  
214 Jon Portnoy <avenj@cobaltirc.org>
215  
216 =cut
217  
218 # vim: ts=2 sw=2 et sts=2 ft=perl
↑ Summary