blib/lib/Earth.pm

filter: Statementstmt 72.9% 62 / 85 Branchbran 40.0% 16 / 40 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 63.1% 12 / 19 Podpod 90.0% 9 / 10 totaltotal 59.5% 103 / 173 SCAR 42.4
CC
26
Cov
60%
chain
20.0
load
16.8
can
12.0
CRAP 69.7
↑ Summary
1 package Earth;
2  
3 1 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @EXPORT = qw(
11 call
12 can
13 chain
14 false
15 make
16 roll
17 then
18 true
19 wrap
20 );
21  
22 1 require Scalar::Util;
23  
24 # STASH
25  
26 1 state $cached = {};
27  
28 # VERSION
29  
30 1 our $VERSION = '0.03';
31  
32 # AUTHORITY
33  
34 1 our $AUTHORITY = 'cpan:AWNCORP';
35  
36 # FUNCTIONS
37  
38 sub call {
39 4 ▶ my ($invocant, $routine, @arguments) = @_;
Subroutine call: called CC=7 SCAR=21.6
Pod documented
40 4 ▶ if (UNIVERSAL::isa($invocant, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($invocant, "CODE")) 0 4 4
41 0 return $invocant->(@arguments);
42 }
43 4 ▶ return if !$routine;
Branch: 1 branch
BranchTrueFalseTotal
unless $routine 0 4 4
44 4 ▶ if (Scalar::Util::blessed($invocant)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($invocant)) 0 4 4
45 0 return $invocant->$routine(@arguments);
46 }
47 4 ▶ if (ref($invocant) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $invocant eq "SCALAR") 1 3 4
48 1 return $$invocant->$routine(@arguments);
49 }
50 3 my $package = load($invocant);
51 3 ▶ if (my $routine = $package->can($routine)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $routine = $package->can($routine)) 0 3 3
52 0 return $routine->(@arguments);
53 }
54 3 ▶ if ($package->can('AUTOLOAD')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->can("AUTOLOAD")) 0 3 3
55 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 0 return &{"${package}::${routine}"}(@arguments);
57 }
58 3 die("call(@{[join(', ', map qq('$_'), @_)]}) failed");
59 }
60  
61 sub can {
62 0 ▶ return if !@_;
Subroutine can: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
63 0 ▶ return call((ref($_[0]) ? $_[0] : \$_[0]), 'can', $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
64 }
65  
66  
67 sub chain {
68 0 ▶ my ($invocant, @routines) = @_;
Subroutine chain: not called CC=4 SCAR=30.0
Pod documented
69 0 ▶ return if !$invocant;
Branch: 1 branch
BranchTrueFalseTotal
unless $invocant 0 0 0
70 0 ▶ for my $next (map +(ref($_) eq 'ARRAY' ? $_ : [$_]), @routines) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
71 0 $invocant = call($invocant, @$next);
72 }
73 0 return $invocant;
74 }
75  
76 sub false {
77 0 ▶ require Scalar::Util;
Subroutine false: not called CC=1 SCAR=6.9
Pod documented
78 0 state $false = Scalar::Util::dualvar(0, "0");
79 }
80  
81 sub load {
82 3 ▶ my ($package) = @_;
Subroutine load: called CC=11 SCAR=28.2
Pod undocumented
83  
84 3 ▶ if ($$cached{$package}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached->{$package}) 2 1 3
85 2 return $package;
86 }
87  
88 1 ▶ if ($package eq 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "main") 0 1 1
89 0 return do {$$cached{$package} = 1; $package};
90 }
91  
92 1 ▶ my $failed = !$package || $package !~ /^\w(?:[\w:']*\w)?$/;
Condition33%1/3
$package && $package =~ /^\w(?:[\w:']*\w)?$/u
!ll&&!rl&&r
001
MC/DC0%0/2
$package && $package =~ /^\w(?:[\w:']*\w)?$/u
$package $package =~ /^\w(?:[\w:']*\w)?$/u
Truth table33%1/3
A && B
A= $packageB= $package =~ /^\w(?:[\w:']*\w)?$/u
ABresult
0X0
100
111
93 1 my $loaded;
94  
95 1 ▶ my $error = do {
Branch: 1 branch
BranchTrueFalseTotal
unless $failed 1 0 1
96 1 local $@;
97 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 $loaded = !!$package->can('new');
99 1 ▶ $loaded = !!$package->can('import') if !$loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 1 0 1
100 1 ▶ $loaded = eval "require $package; 1" if !$loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 1 1
101 1 $@;
102 }
103 if !$failed;
104  
105 1 ▶ die join " ", "Error attempting to load",
Branch: 2 branches
BranchTrueFalseTotal
if $error or $failed or not $loaded 0 1 1
$error ? : 0 0 0
Condition33%2/6
$error or $failed or not $loaded
l!l&&r!l&&!r
001
$error or $failed
l!l&&r!l&&!r
001
MC/DC0%0/3
$error or $failed or not $loaded
$error $failed not $loaded
Truth table25%1/4
A or B or not(C)
A= $errorB= $failedC= not $loaded
ABCresult
1XX1
01X1
0011
0000
106 ($error ? ("$package: $error") : ($package))
107 if $error
108 or $failed
109 or not $loaded;
110  
111 1 $$cached{$package} = 1;
112  
113 1 return $package;
114 }
115  
116 sub make {
117 0 ▶ return if !@_;
Subroutine make: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
118 0 return call($_[0], 'new', @_);
119 }
120  
121 sub roll {
122 0 ▶ return (@_[1,0,2..$#_]);
Subroutine roll: not called CC=1 SCAR=6.9
Pod documented
123 }
124  
125 sub then {
126 0 ▶ return ($_[0], call(@_));
Subroutine then: not called CC=1 SCAR=6.9
Pod documented
127 }
128  
129 sub true {
130 0 ▶ require Scalar::Util;
Subroutine true: not called CC=1 SCAR=6.9
Pod documented
131 0 state $true = Scalar::Util::dualvar(1, "1");
132 }
133  
134 sub wrap {
135 1 ▶ my ($package, $alias) = @_;
Subroutine wrap: called CC=3 SCAR=11.2
Pod documented
136 1 ▶ return if !$package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 1 1
137 1 ▶ my $moniker = $alias // $package =~ s/\W//gr;
Condition33%1/3
$alias // $package =~ s/\W//gru
l!l&&r!l&&!r
100
MC/DC0%0/2
$alias // $package =~ s/\W//gru
$alias $package =~ s/\W//gru
Truth table33%1/3
A // B
A= $aliasB= $package =~ s/\W//gru
ABresult
1X1
011
000
138 1 my $caller = caller(0);
139 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶ return *{"${caller}::${moniker}"} = sub { @_ ? make($package, @_) : $package };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
142 }
143  
144 1 1;
145  
146  
147 =head1 NAME
148  
149 Earth - FP Framework
150  
151 =cut
152  
153 =head1 ABSTRACT
154  
155 FP Framework for Perl 5
156  
157 =cut
158  
159 =head1 VERSION
160  
161 0.03
162  
163 =cut
164  
165 =head1 SYNOPSIS
166  
167 package main;
168  
169 use Earth;
170  
171 wrap 'Digest::SHA', 'SHA';
172  
173 call(SHA(), 'sha1_hex');
174  
175 # "da39a3ee5e6b4b0d3255bfef95601890afd80709"
176  
177 =cut
178  
179 =head1 DESCRIPTION
180  
181 Earth is a functional-programming framework for Perl 5. Perl is a
182 multi-paradigm programming language that also supports functional programming,
183 but, Perl has an intentionally limited standard library with an emphasis on
184 providing library support via the CPAN which is overwhelmingly object-oriented.
185 This makes developing in a functional style difficult as you'll eventually need
186 to rely on a CPAN library that requires you to switch over to object-oriented
187 programming. Earth facilitates functional programming for Perl 5 by providing
188 functions which enable indirect routine dispatching, allowing the execution of
189 both functional and object-oriented code.
190  
191 =cut
192  
193 =head1 FUNCTIONS
194  
195 This package provides the following functions:
196  
197 =cut
198  
199 =head2 call
200  
201 call(Str | Object | CodeRef $self, Any @args) (Any)
202  
203 The call function dispatches function and method calls to a package and returns
204 the result.
205  
206 I<Since C<0.01>>
207  
208 =over 4
209  
210 =item call example 1
211  
212 # given: synopsis
213  
214 call(SHA, 'sha1_hex');
215  
216 # "da39a3ee5e6b4b0d3255bfef95601890afd80709"
217  
218 =back
219  
220 =over 4
221  
222 =item call example 2
223  
224 # given: synopsis
225  
226 call('Digest::SHA', 'sha1_hex');
227  
228 # "da39a3ee5e6b4b0d3255bfef95601890afd80709"
229  
230 =back
231  
232 =over 4
233  
234 =item call example 3
235  
236 # given: synopsis
237  
238 call(\SHA, 'new');
239  
240 # bless(do{\(my $o = '...')}, 'Digest::SHA')
241  
242 =back
243  
244 =over 4
245  
246 =item call example 4
247  
248 # given: synopsis
249  
250 wrap 'Digest';
251  
252 call(Digest('SHA'), 'reset');
253  
254 # "da39a3ee5e6b4b0d3255bfef95601890afd80709"
255  
256 =back
257  
258 =cut
259  
260 =head2 can
261  
262 can(Str | Object | CodeRef $self, Str $name) (CodeRef)
263  
264 The can function checks if the object or class has a routine matching the name
265 provided, and if so returns a coderef for that routine.
266  
267 I<Since C<0.02>>
268  
269 =over 4
270  
271 =item can example 1
272  
273 # given: synopsis
274  
275 my $coderef = can(SHA(1), 'sha1_hex');
276  
277 # sub { ... }
278  
279 =back
280  
281 =cut
282  
283 =head2 chain
284  
285 chain(Str | Object | CodeRef $self, Str | ArrayRef[Str] @args) (Any)
286  
287 The chain function chains function and method calls to a package (and return
288 values) and returns the result.
289  
290 I<Since C<0.01>>
291  
292 =over 4
293  
294 =item chain example 1
295  
296 # given: synopsis
297  
298 my $hex = chain(\SHA, 'new', 'sha1_hex');
299  
300 # "d3aed913fdc7f277dddcbde47d50a8b5259cb4bc"
301  
302 =back
303  
304 =over 4
305  
306 =item chain example 2
307  
308 # given: synopsis
309  
310 my $hex = chain(\SHA, 'new', ['add', 'hello'], 'sha1_hex');
311  
312 # "f47b0cd4b6336d07ab117d7ee3f47566c9799f23"
313  
314 =back
315  
316 =over 4
317  
318 =item chain example 3
319  
320 # given: synopsis
321  
322 wrap 'Digest';
323  
324 my $hex = chain(Digest('SHA'), ['add', 'hello'], 'sha1_hex');
325  
326 # "8575ce82b266fdb5bc98eb43488c3b420577c24c"
327  
328 =back
329  
330 =cut
331  
332 =head2 false
333  
334 false() (Bool)
335  
336 The false function returns a falsy boolean value which is designed to be
337 practically indistinguishable from the conventional numerical C<0> value.
338  
339 I<Since C<0.01>>
340  
341 =over 4
342  
343 =item false example 1
344  
345 package main;
346  
347 use Earth;
348  
349 my $false = false;
350  
351 # 0
352  
353 =back
354  
355 =over 4
356  
357 =item false example 2
358  
359 package main;
360  
361 use Earth;
362  
363 my $true = !false;
364  
365 # 1
366  
367 =back
368  
369 =cut
370  
371 =head2 make
372  
373 make(Str $package, Any @args) (Any)
374  
375 The make function L<"calls"|Earth/call> the C<new> routine on the invocant and
376 returns the result which should be a package string or an object.
377  
378 I<Since C<0.01>>
379  
380 =over 4
381  
382 =item make example 1
383  
384 # given: synopsis
385  
386 my $string = make(SHA);
387  
388 # bless(do{\(my $o = '...')}, 'Digest::SHA')
389  
390 =back
391  
392 =over 4
393  
394 =item make example 2
395  
396 # given: synopsis
397  
398 my $string = make(Digest, 'SHA');
399  
400 # bless(do{\(my $o = '...')}, 'Digest::SHA')
401  
402 =back
403  
404 =cut
405  
406 =head2 roll
407  
408 roll(Str $name, Any @args) (Any)
409  
410 The roll function takes a list of arguments, assuming the first argument is
411 invokable, and reorders the list such that the routine name provided comes
412 after the invocant (i.e. the 1st argument), creating a list acceptable to the
413 L</call> function.
414  
415 I<Since C<0.02>>
416  
417 =over 4
418  
419 =item roll example 1
420  
421 package main;
422  
423 use Earth;
424  
425 my @list = roll('sha1_hex', SHA);
426  
427 # ("Digest::SHA", "sha1_hex")
428  
429 =back
430  
431 =over 4
432  
433 =item roll example 2
434  
435 package main;
436  
437 use Earth;
438  
439 my @list = roll('sha1_hex', call(SHA(1), 'reset'));
440  
441 # (bless(do{\(my $o = '...')}, 'Digest::SHA'), "sha1_hex")
442  
443 =back
444  
445 =cut
446  
447 =head2 then
448  
449 then(Str | Object | CodeRef $self, Any @args) (Any)
450  
451 The then function proxies the call request to the L</call> function and returns
452 the result as a list, prepended with the invocant.
453  
454 I<Since C<0.02>>
455  
456 =over 4
457  
458 =item then example 1
459  
460 package main;
461  
462 use Earth;
463  
464 my @list = then(SHA, 'sha1_hex');
465  
466 # ("Digest::SHA", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
467  
468 =back
469  
470 =cut
471  
472 =head2 true
473  
474 true() (Bool)
475  
476 The true function returns a truthy boolean value which is designed to be
477 practically indistinguishable from the conventional numerical C<1> value.
478  
479 I<Since C<0.01>>
480  
481 =over 4
482  
483 =item true example 1
484  
485 package main;
486  
487 use Earth;
488  
489 my $true = true;
490  
491 # 1
492  
493 =back
494  
495 =over 4
496  
497 =item true example 2
498  
499 package main;
500  
501 use Earth;
502  
503 my $false = !true;
504  
505 # 0
506  
507 =back
508  
509 =cut
510  
511 =head2 wrap
512  
513 wrap(Str $package, Str $alias) (CodeRef)
514  
515 The wrap function installs a wrapper function in the calling package which when
516 called either returns the package string if no arguments are provided, or calls
517 L</make> on the package with whatever arguments are provided and returns the
518 result. Unless an alias is provided as a second argument, special characters
519 are stripped from the package to create the function name.
520  
521 I<Since C<0.01>>
522  
523 =over 4
524  
525 =item wrap example 1
526  
527 # given: synopsis
528  
529 my $coderef = wrap('Digest::SHA');
530  
531 # my $digest = DigestSHA();
532  
533 # "Digest::SHA"
534  
535 =back
536  
537 =over 4
538  
539 =item wrap example 2
540  
541 # given: synopsis
542  
543 my $coderef = wrap('Digest::SHA');
544  
545 # my $digest = DigestSHA(1);
546  
547 # bless(do{\(my $o = '...')}, 'Digest::SHA')
548  
549 =back
550  
551 =over 4
552  
553 =item wrap example 3
554  
555 # given: synopsis
556  
557 my $coderef = wrap('Digest::SHA', 'SHA');
558  
559 # my $digest = SHA;
560  
561 # "Digest::SHA"
562  
563 =back
564  
565 =over 4
566  
567 =item wrap example 4
568  
569 # given: synopsis
570  
571 my $coderef = wrap('Digest::SHA', 'SHA');
572  
573 # my $digest = SHA(1);
574  
575 # bless(do{\(my $o = '...')}, 'Digest::SHA')
576  
577 =back
578  
579 =cut
580  
581 =head1 AUTHORS
582  
583 Awncorp, C<awncorp@cpan.org>
584  
585 =cut
↑ Summary