lib/FP/Struct.pm

filter: Statementstmt 95.3% 247 / 259 Branchbran 63.2% 62 / 98 Conditioncond 57.1% 12 / 21 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 97.5% 40 / 41 Podpod 0.0% 0 / 7 totaltotal 82.9% 365 / 440 CC 65 SCAR 18.4
CC
65
Cov
85%
__ANON__
14.4
import
10.2
unacceptable_value_error
10.0
CRAP 6.3
« .htmlgen/FunctionalPerl/Htmlgen/PerlTidy.pm ↑ Summary lib/FP/Git/Repository.pm »
1 #
2 # Copyright (c) 2013-2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Struct - classes for functional perl
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Predicates qw(is_array maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶ use FP::Struct 'FPStructExample::Foo' =>
Subroutine BEGIN: called CC=1 SCAR=0.0
19 ["name", # accept any value
20 [maybe (\&is_array), "animals"], # accept arrays or undef
21 ]
22 # => "Baz", "Buzz" # optional superclasses
23 1 ;
24  
25 # creates a constructor new that takes positional arguments and
26 # copies them to a hash with the keys "name" and "animals". Also,
27 # sets @Bar::ISA to ("Baz", "Buzz") if the '#' is removed. [ ] around
28 # "Baz", "Buzz" are optional. If an array is given as a field
29 # declaration, then the first entry is a predicate that receives the
30 # value in question, if it doesn't return true then an exception is
31 # thrown.
32  
33 is( new FPStructExample::Foo ("Tim")->name, "Tim" );
34 eval {
35 new FPStructExample::Foo ("Tim", 0)
36 };
37 like $@, qr/^unacceptable value for field 'animals': 0 /;
38 is (new FPStructExample::Foo (undef, ["Struppi"])->animals->[0], "Struppi");
39 is (new_ FPStructExample::Foo (animals => ["Struppi"])->animals->[0], "Struppi");
40  
41  
42 # Usually preferred alternative: define the struct from within the
43 # package:
44  
45 # a mixin package, if this weren't defined at the time of 'use
46 # FP::Struct' below, it would try to load Hum.pm
47 package FPStructExample::Hum {
48 1 ▶ sub hum {
Subroutine hum: called CC=1 SCAR=0.0
49 1 my $s = shift;
50 1 $s->name." hums ".$s->a." over ".$s->b
51 }
52 }
53 package FPStructExample::Hah {
54 1 ▶ use FP::Struct ["aa"];
Subroutine BEGIN: called CC=1 SCAR=0.0
55 _END_
56 }
57  
58 package FPStructExample::Bar2 {
59  
60 1 ▶ use Chj::TEST; # the TEST sub will be removed from the package upon
Subroutine BEGIN: called CC=1 SCAR=0.0
61 # _END_ (namespace cleaning)
62 1 ▶ use FP::Struct ["a","b"] => "FPStructExample::Foo",
Subroutine BEGIN: called CC=1 SCAR=0.0
63 "FPStructExample::Hum",
64 1 "FPStructExample::Hah";
65 8 ▶ sub div {
Subroutine div: called CC=1 SCAR=0.0
66 8 my $s = shift;
67 $$s{a} / $$s{b}
68 8 }
69 TEST { FPStructExample::Bar2->new_(a => 1, b => 2)->div } 1/2;
70 _END_ # generate accessors for methods of given name which don't
71 # exist yet *in either Bar or any super class*. (Does that
72 # make sense?)
73 # and export the constructors to the packages given here:
74 "main";
75 }
76  
77 package main {
78 1 ▶ use FP::Equal 'is_equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 my $bar = new FPStructExample::Bar2 ("Franz", ["Barney"], "some aa", 1,2);
81 # same thing, but with sub instead of method call interface:
82 my $baz = FPStructExample::Bar2::c::Bar2 ("Franz", ["Barney"], "some aa", 1,2);
83 # or:
84 #FPStructExample::Bar2::constructors->import;
85 # or in fact, we already exported them via _END_("main")
86 my $baz = Bar2 ("Franz", ["Barney"], "some aa", 1,2);
87  
88 is $bar->div, 1/2;
89  
90 is(Bar2_(a => 1,b => 2)->div, 1/2);
91 is(FPStructExample::Bar2::c::Bar2_(a => 1, b => 2)->div, 1/2);
92 is(new__ FPStructExample::Bar2({a => 1,b => 2})->div, 1/2);
93 is(unsafe_new__ FPStructExample::Bar2({a => 1,b => 2})->div, 1/2);
94 # NOTE: unsafe_new__ returns the argument hash after checking and
95 # blessing it, it doesn't copy it! Be careful. `new__` does copy it.
96  
97 is $bar->b_set(3)->div, 1/3;
98  
99 1 ▶ use FP::Div 'inc';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 is $bar->b_update(\&inc)->div, 1/3;
101  
102 is $bar->hum, "Franz hums 1 over 2";
103  
104 is Chj::TEST::run_tests("FPStructExample::Bar2")->successes, 1;
105 is (FPStructExample::Bar2->can("TEST"), undef);
106 # ^ it was removed by namespace cleaning
107  
108 # Meta information: what was given in the field definitions,
109 # for the class and all FP::Struct based super classes:
110 my @fd = FP::Struct::all_fields(["FPStructExample::Bar2"]);
111 my @fieldnames = map { ref $_ ? $_->[1] : $_ } @fd;
112 is_equal \@fieldnames,
113 [ 'name', 'animals', 'aa', 'a', 'b' ];
114 # Just the field names, directly:
115 is_equal [ FP::Struct::all_field_names(["FPStructExample::Bar2"]) ],
116 [ 'name', 'animals', 'aa', 'a', 'b' ];
117 }
118  
119 =for test ignore
120  
121 =head1 DESCRIPTION
122  
123 Create functional setters (i.e. setters that return a copy of the
124 object so as to leave the original unharmed), take predicate functions
125 (not magic strings) for dynamic type checking, simpler than
126 Class::Struct.
127  
128 Also creates constructor methods: C<new> that takes positional
129 arguments, C<new_> which takes name => value pairs, C<new__> which takes
130 a hash with name => value pairs as a single argument, and
131 C<unsafe_new__> which does the same as C<new__> but reuses the given
132 hash (unsafe if the latter is modified later on).
133  
134 Also creates constructor functions (i.e. subroutine instead of method
135 calling interface) C<Foo::Bar::c::Bar()> for positional and
136 C<Foo::Bar::c::Bar_()> for named arguments for package Foo::Bar. These
137 are also in C<Foo::Bar::constructors::> and can be imported using
138 (without arguments, it imports both):
139  
140 Foo::Bar::constructors->import(qw(Bar Bar_));
141  
142 C<_END_> does namespace cleaning: any sub that was defined before the C<use
143 FP::Struct> call is removed by the C<_END_> call (those that are not the
144 same sub ref anymore, i.e. have been redefined, are left
145 unchanged). This means that if the C<use FP::Struct> statement is put
146 after any other (procedure-importing) 'use' statement, but before the
147 definition of the methods, that the imported procedures can be used
148 from within the defined methods, but are not around afterwards,
149 i.e. they will not shadow super class methods. (Thanks to Matt S Trout
150 for pointing out the idea.) To avoid the namespace cleaning, write
151 C<_END__> instead of C<_END_>. Both of these optionally take any number
152 of packages, to which they will export the constructors.
153  
154 See L<FP::Predicates> for some useful predicates (others are in the
155 respective modules that define them, like C<is_pair> in L<FP::List>).
156  
157 =head1 UTILITY BASE CLASSES
158  
159 =head2 PURITY
160  
161 It is recommended to use L<FP::Abstract::Pure> as a base class. This
162 means objects from classes based on FP::Struct are automatically
163 treated as pure by C<is_pure> from L<FP::Predicates>.
164  
165 If C<$FP::Struct::immutable> is true (default), then if
166 L<FP::Abstract::Pure> is inherited the objects are made immutable to
167 ensure purity.
168  
169 =head2 FP::Struct specific
170  
171 To avoid having to implement some protocols manually, some base
172 classes are provided to auto-derive those protocols automatically:
173 <FP::Struct::Show>, <FP::Struct::Equal>. They use the knowledge that
174 <FP::Struct> has about the fields in the class to make the protocol
175 consider all the fields automatically.
176  
177 =head2 :defaults
178  
179 If ":defaults" is given as a base class, it is expanded to those base
180 classes that should always work and should always be
181 useful. Currently, these are <FP::Struct::Show>, <FP::Struct::Equal>.
182  
183 =head1 ALSO SEE
184  
185 L<FP::Abstract::Pure>, <FP::Struct::Show>, <FP::Struct::Equal>
186  
187 =head1 NOTE
188  
189 This is alpha software! Read the status section in the package README
190 or on the L<website|http://functional-perl.org/>.
191  
192 =cut
193  
194 # XX todo: solve mutable private fields (which would leave those
195 # mutable, but still allow to inherit Pure). Deal with these thoughts:
196 # "To hold this promise true, your code must not mutate any object fields
197 # except when it's impossible for the outside world to detect
198 # (e.g. using a hash key to hold a cached result is fine as long as you
199 # also override all the functional setters for fields that are used for
200 # the calculation of the cached value to clean the cache (TODO: provide
201 # option to turn off generation of setters, and/or provide hook (for
202 # cloning?)).)"
203  
204 package FP::Struct;
205  
206 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 61 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
209 61 ▶use Chj::NamespaceClean;
Subroutine BEGIN: called CC=1 SCAR=0.0
210 61 ▶use FP::Show qw(show);
Subroutine BEGIN: called CC=1 SCAR=0.0
211 61 ▶use FP::Interface qw(require_package package_check_possible_interface);
Subroutine BEGIN: called CC=1 SCAR=0.0
212 61 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 61 ▶use Safe::Isa; # $_can
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 1520 ▶sub all_fields {
Subroutine all_fields: called CC=2 SCAR=7.0
Pod undocumented
216 1520 my ($isa) = @_;
217 (
218 map {
219 1520 my ($package) = $_;
220 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 1257 ▶ if (my $fields = \@{"${package}::__Struct__fields"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fields = \@{"${package}::__Struct__fields";}) { } 1257 0 1257
222 1257 (all_fields(\@{"${package}::ISA"}), @$fields)
223 } else {
224 () # don't even look at parent classes in that case, is
225 # that reasonable?
226 0 }
227 } @$isa
228 )
229 }
230  
231 1 ▶sub all_field_names {
Subroutine all_field_names: called CC=2 SCAR=6.9
Pod undocumented
232 1 my ($isa) = @_;
233 1 ▶ map { ref $_ ? $_->[1] : $_ } all_fields($isa)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 4 5
234 }
235  
236 778 ▶sub field_maybe_predicate {
Subroutine field_maybe_predicate: called CC=3 SCAR=11.1
Pod undocumented
237 778 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 778 0 778
238 778 my ($s) = @_;
239 778 ▶ (ref $s) ? $$s[0] : undef
Branch: 1 branch
BranchTrueFalseTotal
ref $s ? : 458 320 778
240 }
241  
242 1131 ▶sub field_name {
Subroutine field_name: called CC=3 SCAR=11.1
Pod undocumented
243 1131 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1131 0 1131
244 1131 my ($s) = @_;
245 1131 ▶ (ref $s) ? $$s[1] : $s
Branch: 1 branch
BranchTrueFalseTotal
ref $s ? : 740 391 1131
246 }
247  
248 # returns nothing at all if a predicate was given but is undef
249 278 ▶sub field_maybe_predicate_and_name {
Subroutine field_maybe_predicate_and_name: called CC=4 SCAR=14.3
Pod undocumented
250 278 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 278 0 278
251 278 my ($s) = @_;
252 278 ▶ (ref $s) ? (defined($$s[0]) ? @$s : ()) : (undef, $s)
Branch: 2 branches
BranchTrueFalseTotal
ref $s ? : 156 122 278
defined $s->[0] ? : 156 0 156
253 }
254  
255 0 ▶sub field_has_predicate {
Subroutine field_has_predicate: not called CC=2 SCAR=17.9
Pod undocumented
256 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
257 0 my ($s) = @_;
258 0 ref $s
259 }
260  
261 61 our $immutable = 1; # only used if also is_pure
262  
263 61 my $warned_once;
264  
265 5 ▶sub unacceptable_value_error {
Subroutine unacceptable_value_error: called CC=4 SCAR=23.1
Pod undocumented
266 5 my ($failure, $fieldname, $value) = @_;
267 my $failstr = ref($failure)
268 5 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
ref $failure ? : 0 5 5
269  
270 # quack like a duck, as long as there's no
271 # `FP::Abstract::Failure` protocol
272 0 ▶ if (my $message = $failure->$_can("message")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = $failure->$FP::Struct::_can('message')) { } 0 0 0
273 0 my $str = $message->($failure);
274 0 chomp $str;
275 0 " ($str)"
276 } else {
277 0 ▶ unless ($warned_once) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned_once) 0 0 0
278 0 warn
279 "warning once: got an falsey object with no `message` method: $failure";
280 0 $warned_once++;
281 }
282 ""
283 0 }
284 }
285 : "";
286 5 @_ = ("unacceptable value for field '$fieldname'$failstr: " . show($value));
287 5 goto \&fp_croak;
288 }
289  
290 214 ▶sub import {
Subroutine import: called CC=10 SCAR=23.2
291 214 my $_importpackage = shift;
292 214 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 214 214
293 214 my ($package, $is_expandedvariant, $fields, @perhaps_isa);
294 214 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 213 1 214
295 213 ($fields, @perhaps_isa) = @_;
296 213 $package = caller;
297 213 $is_expandedvariant = 1;
298 } else {
299 1 ($package, $fields, @perhaps_isa) = @_;
300 1 $is_expandedvariant = 0;
301 }
302 my @_isa
303 214 ▶ = (@perhaps_isa == 1 and ref($perhaps_isa[0]))
Branch: 1 branch
BranchTrueFalseTotal
@perhaps_isa == 1 && ref $perhaps_isa[0] ? : 0 214 214
Condition66%2/3
@perhaps_isa == 1 and ref $perhaps_isa[0]
!ll&&!rl&&r
119950
MC/DC0%0/2
@perhaps_isa == 1 and ref $perhaps_isa[0]
@perhaps_isa == 1 ref $perhaps_isa[0]
Truth table66%2/3
A and B
A= @perhaps_isa == 1B= ref $perhaps_isa[0]
ABresult
0X0
100
111
304 ? $perhaps_isa[0]
305 : @perhaps_isa;
306  
307 my @isa = map {
308 214 ▶ ( $_ eq ":defaults" ? qw(FP::Struct::Show FP::Struct::Equal)
Branch: 2 branches
BranchTrueFalseTotal
$_ eq ':defaults' ? : 0 295 295
$_ eq ':default' ? : 0 295 295
309 : $_ eq ":default" ? fp_croak
310 "base class ':default' is invalid, did you mean ':defaults'?"
311 : $_)
312 } @_isa;
313  
314 214 require_package $_ for @isa;
315 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
316 214 *{"${package}::ISA"} = \@isa;
317  
318 214 my $is_pure = $package->isa("FP::Abstract::Pure");
319  
320 214 my $allfields = [all_fields(\@isa), @$fields];
321  
322 # (^ ah, could store them in the package as well; but well, no
323 # worries)
324 214 my $allfields_name = [map { field_name $_} @$allfields];
325  
326 # get list of package entries *before* setting
327 # accessors/constructors
328 214 my $nonmethods = package_keys $package;
329  
330 214 my @package_parts = split /::/, $package;
331 214 my $package_lastpart = $package_parts[-1];
332  
333 # constructor with positional parameters:
334 214 my $allfields_i_with_predicate = do {
335 214 my $i = -1;
336 [
337 map {
338 214 $i++;
339 389 ▶ if (my $pred = field_maybe_predicate $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pred = field_maybe_predicate($_)) { } 229 160 389
340 229 [$pred, field_name($_), $i]
341 } else {
342 ()
343 160 }
344 } @$allfields
345 ]
346 };
347 214 ▶ *{"${package}::new"} = sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
348 312 my $class = shift;
349 312 ▶ @_ <= @$allfields or fp_croak "too many arguments to ${package}::new";
Branch: 1 branch
BranchTrueFalseTotal
@_ <= @$allfields or fp_croak("too many arguments to ${package}::new") 312 0 312
350 312 for (@$allfields_i_with_predicate) {
351 89 my ($pred, $name, $i) = @$_;
352 89 my $possibly_fail = &$pred($_[$i]);
353 89 ▶ $possibly_fail
Branch: 1 branch
BranchTrueFalseTotal
$possibly_fail or unacceptable_value_error($possibly_fail, $name, $_[$i]) 84 5 89
354 or unacceptable_value_error($possibly_fail, $name, $_[$i]);
355 }
356 307 my %s;
357 307 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 803 307 1110
358 803 my $fieldname = $$allfields_name[$i];
359 803 $s{$fieldname} = $_[$i];
360 803 ▶ Internals::SvREADONLY $s{$fieldname}, 1 if $is_pure && $immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pure and $FP::Struct::immutable 738 65 803
Condition66%2/3
$is_pure and $FP::Struct::immutable
!ll&&!rl&&r
650738
MC/DC50%1/2
$is_pure and $FP::Struct::immutable
$is_pure $FP::Struct::immutable
Truth table66%2/3
A and B
A= $is_pureB= $FP::Struct::immutable
ABresult
0X0
100
111
361 }
362 307 my $s = bless \%s, $class;
363 307 ▶ Internals::SvREADONLY %$s, 1 if $is_pure && $immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pure and $FP::Struct::immutable 283 24 307
Condition66%2/3
$is_pure and $FP::Struct::immutable
!ll&&!rl&&r
240283
MC/DC50%1/2
$is_pure and $FP::Struct::immutable
$is_pure $FP::Struct::immutable
Truth table66%2/3
A and B
A= $is_pureB= $FP::Struct::immutable
ABresult
0X0
100
111
364 307 $s
365 214 };
366 214 ▶ *{"${package}::c::${package_lastpart}"} = my $constructor = sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
367  
368 # XX bah, almost copy-paste, because want to avoid sub call
369 # overhead (inlining please finally?):
370 2686 ▶ @_ <= @$allfields or fp_croak "too many arguments to ${package}::new";
Branch: 1 branch
BranchTrueFalseTotal
@_ <= @$allfields or fp_croak("too many arguments to ${package}::new") 2686 0 2686
371 2686 for (@$allfields_i_with_predicate) {
372 202 my ($pred, $name, $i) = @$_;
373 202 my $possibly_fail = &$pred($_[$i]);
374 202 ▶ $possibly_fail
Branch: 1 branch
BranchTrueFalseTotal
$possibly_fail or unacceptable_value_error($possibly_fail, $name, $_[$i]) 202 0 202
375 or unacceptable_value_error($possibly_fail, $name, $_[$i]);
376 }
377 2686 my %s;
378 2686 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 7992 2686 10678
379 7992 my $fieldname = $$allfields_name[$i];
380 7992 $s{$fieldname} = $_[$i];
381 7992 ▶ Internals::SvREADONLY $s{$fieldname}, 1 if $is_pure && $immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pure and $FP::Struct::immutable 334 7658 7992
Condition66%2/3
$is_pure and $FP::Struct::immutable
!ll&&!rl&&r
76580334
MC/DC50%1/2
$is_pure and $FP::Struct::immutable
$is_pure $FP::Struct::immutable
Truth table66%2/3
A and B
A= $is_pureB= $FP::Struct::immutable
ABresult
0X0
100
111
382 }
383 2686 my $s = bless \%s, $package;
384 2686 ▶ Internals::SvREADONLY %$s, 1 if $is_pure && $immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pure and $FP::Struct::immutable 233 2453 2686
Condition66%2/3
$is_pure and $FP::Struct::immutable
!ll&&!rl&&r
24530233
MC/DC50%1/2
$is_pure and $FP::Struct::immutable
$is_pure $FP::Struct::immutable
Truth table66%2/3
A and B
A= $is_pureB= $FP::Struct::immutable
ABresult
0X0
100
111
385 2686 $s
386 214 };
387  
388 # constructor with keyword/value parameters:
389 214 my $allfields_h = +{ map { field_name($_) => undef } @$allfields };
390 my $allfields_with_predicate
391 214 = [grep { field_maybe_predicate $_ } @$allfields];
392 214 ▶ *{"${package}::new_"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
393 47 my $class = shift;
394 47 $class->unsafe_new__(+{@_})
395 214 };
396  
397 # XX mostly-copy-pasting again (like above):
398 214 ▶ *{"${package}::c::${package_lastpart}_"} = my $constructor_ = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
399 2 $package->unsafe_new__(+{@_})
400 214 };
401  
402 # constructor with hash parameter:
403 214 ▶ *{"${package}::new__"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
404 2 my $class = shift;
405 2 ▶ @_ == 1 or fp_croak "wrong number of arguments to ${package}::new__";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak("wrong number of arguments to ${package}::new__") 2 0 2
406 2 my ($h) = @_;
407 2 $class->unsafe_new__(+{%$h})
408 214 ▶ }, *{"${package}::unsafe_new__"} = sub {
Subroutine __ANON__: called CC=11 SCAR=26.6
409  
410 # NOTE: reuses (blesses) the argument hash! careful!
411 52 my $class = shift;
412 52 ▶ @_ == 1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak("wrong number of arguments to ${package}::unsafe_new__") 52 0 52
413 or fp_croak "wrong number of arguments to ${package}::unsafe_new__";
414 52 my ($s) = @_;
415 52 ▶ scalar(keys %$s) <= (@$allfields * 2)
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$s <= @$allfields * 2 or fp_croak("too many arguments to ${package}::new_") 52 0 52
416 or fp_croak "too many arguments to ${package}::new_";
417 52 for (keys %$s) {
418 150 ▶ exists $$allfields_h{$_} or fp_croak "unknown field '$_'";
Branch: 1 branch
BranchTrueFalseTotal
exists $allfields_h->{$_} or fp_croak("unknown field '${_}'") 150 0 150
419 150 ▶ Internals::SvREADONLY $$s{$_}, 1 if $is_pure && $immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pure and $FP::Struct::immutable 0 150 150
Condition33%1/3
$is_pure and $FP::Struct::immutable
!ll&&!rl&&r
15000
MC/DC0%0/2
$is_pure and $FP::Struct::immutable
$is_pure $FP::Struct::immutable
Truth table33%1/3
A and B
A= $is_pureB= $FP::Struct::immutable
ABresult
0X0
100
111
420 }
421 52 for (@$allfields_with_predicate) {
422 165 my ($pred, $name) = @$_;
423 165 my $possibly_fail = &$pred($$s{$name});
424 $possibly_fail
425 165 ▶ or unacceptable_value_error($possibly_fail, $name, $$s{$name});
Branch: 1 branch
BranchTrueFalseTotal
$possibly_fail or unacceptable_value_error($possibly_fail, $name, $s->{$name}) 165 0 165
426 }
427 52 bless $s, $class;
428 52 ▶ Internals::SvREADONLY %$s, 1 if $is_pure && $immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pure and $FP::Struct::immutable 0 52 52
Condition33%1/3
$is_pure and $FP::Struct::immutable
!ll&&!rl&&r
5200
MC/DC0%0/2
$is_pure and $FP::Struct::immutable
$is_pure $FP::Struct::immutable
Truth table33%1/3
A and B
A= $is_pureB= $FP::Struct::immutable
ABresult
0X0
100
111
429 52 $s
430 214 };
431  
432 # constructor exports: -- XX why did I decide to not use ::c:: for this? historic?
433 214 *{"${package}::constructors::${package_lastpart}"} = $constructor;
434 214 *{"${package}::constructors::${package_lastpart}_"} = $constructor_;
435 214 *{"${package}::constructors::ISA"} = ["Exporter"];
436 214 my $exports = [$package_lastpart, "${package_lastpart}_"];
437 214 *{"${package}::constructors::EXPORT"} = $exports;
438 214 *{"${package}::constructors::EXPORT_OK"} = [];
439 214 *{"${package}::constructors::EXPORT_TAGS"} = +{ all => $exports };
440  
441 214 ▶ my $end = sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
442 214 my (@exportpackages) = @_;
443  
444 #warn "_END_ called for package '$package'";
445 214 for my $_field (@$fields) {
446 278 ▶ my ($maybe_predicate, $name)
Branch: 2 branches
BranchTrueFalseTotal
() = field_maybe_predicate_and_name($_field) or fp_croak('type predicate given but undef (this can happen due to phasing, e.g. referring to a lexical variable defined in the same file) for field ' . (defined $_field->[1] ? "'$_field->[1]'" : "undef")) 278 0 278
defined $_field->[1] ? : 0 0 0
447 = field_maybe_predicate_and_name($_field)
448 or fp_croak "type predicate given but undef (this can happen "
449 . "due to phasing, e.g. referring to a lexical variable "
450 . "defined in the same file) for field "
451 . (defined($$_field[1]) ? "'$$_field[1]'" : "undef");
452  
453 # accessors
454 278 ▶ if (not $package->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->can($name)) 273 5 278
455 273 ▶ *{"${package}::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
456 3705 my $s = shift;
457 3705 $$s{$name}
458 273 };
459 }
460  
461 # functional modifiers
462 556 ▶ my $add_modifier = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
463 556 my ($modifierappendix, $modifier) = @_;
464 556 my $modifiername = "$name$modifierappendix";
465 556 ▶ unless ($package->can($modifiername)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->can($modifiername)) 556 0 556
466 556 *{"${package}::$modifiername"} = $modifier;
467 }
468 278 };
469  
470 &$add_modifier(
471 "_set",
472 $maybe_predicate
473 5 ▶ ? sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
474 5 my $s = shift;
475 5 ▶ @_ == 1 or fp_croak "${name}_set: need 1 argument";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak("${name}_set: need 1 argument") 5 0 5
476 5 my $v = shift;
477 5 my $possibly_fail = &$maybe_predicate($v);
478 5 ▶ $possibly_fail
Branch: 1 branch
BranchTrueFalseTotal
$possibly_fail or unacceptable_value_error($possibly_fail, $name, $v) 5 0 5
479 or unacceptable_value_error($possibly_fail, $name, $v);
480 5 my $new = +{%$s};
481 5 $$new{$name} = $v;
482 5 bless $new, ref $s
483 }
484 196 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
485 196 my $s = shift;
486 196 ▶ @_ == 1 or fp_croak "${name}_set: need 1 argument";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak("${name}_set: need 1 argument") 196 0 196
487 196 my $new = +{%$s};
488 196 ($$new{$name}) = @_;
489 196 bless $new, ref $s
490 }
491 278 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$maybe_predicate ? : 156 122 278
492  
493 &$add_modifier(
494 "_update",
495 $maybe_predicate
496 14 ▶ ? sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
497 14 ▶ @_ == 2 or fp_croak "${name}_update: need 2 arguments";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak("${name}_update: need 2 arguments") 14 0 14
498 14 my ($s, $fn) = @_;
499 14 my $v = &$fn($s->{$name});
500 14 my $possibly_fail = &$maybe_predicate($v);
501 14 ▶ $possibly_fail
Branch: 1 branch
BranchTrueFalseTotal
$possibly_fail or unacceptable_value_error($possibly_fail, $name, $v) 14 0 14
502 or unacceptable_value_error($possibly_fail, $name, $v);
503 14 my $new = +{%$s};
504 14 $$new{$name} = $v;
505 14 bless $new, ref $s
506 }
507 6 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
508 6 ▶ @_ == 2 or fp_croak "${name}_update: need 2 arguments";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak("${name}_update: need 2 arguments") 6 0 6
509 6 my ($s, $fn) = @_;
510 6 my $v = &$fn($s->{$name});
511 6 my $new = +{%$s};
512 6 ($$new{$name}) = $v;
513 6 bless $new, ref $s
514 }
515 278 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$maybe_predicate ? : 156 122 278
516 }
517  
518 # Optionally export to packages:
519 214 for my $exportpackage (@exportpackages) {
520  
521 # warn "exporting constructors from $package to $exportpackage";
522 10 *{"${exportpackage}::${package_lastpart}"} = $constructor;
523 10 *{"${exportpackage}::${package_lastpart}_"} = $constructor_;
524 }
525  
526 # Check any interfaces:
527 214 package_check_possible_interface($package, $_) for @isa;
528  
529 214 1 # make module load succeed at the same time.
530 214 };
531 214 *{"${package}::_END__"} = $end;
532 214 ▶ *{"${package}::_END_"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
533  
534 #warn "_END_ called for package '$package'";
535 211 package_delete $package, $nonmethods;
536 211 goto $end;
537 214 };
538  
539 214 ▶ unless ($is_expandedvariant) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_expandedvariant) 1 213 214
540  
541 # Not expecting the user to write methods, finalize
542 # immediately.
543 1 &$end()
544 }
545  
546 214 *{"${package}::__Struct__fields"} = $fields;
547  
548 }
549  
550 61 1
« .htmlgen/FunctionalPerl/Htmlgen/PerlTidy.pm ↑ Summary lib/FP/Git/Repository.pm »