File Coverage

blib/lib/Sub/Composable.pm
Criterion Covered Total %
statement 23 24 95.8
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 0 3 0.0
total 34 39 87.1


line stmt bran cond sub pod time code
1 10     10   4461 use strict; use warnings;
  10     10   35  
  10         436  
  10         72  
  10         14  
  10         808  
2             package Sub::Composable;
3             $Sub::Composable::VERSION = '0.14';
4 10     10   5065 use Sub::Name;
  10         7558  
  10         3152  
5              
6             # use Sub::Compose qw( chain ); # doesn't fucking work, due to scalar/list context shenanigans
7              
8             sub compose {
9 5     5 0 153738 my ($l, $r, $swap) = @_;
10              
11 5 100       16 if ($swap) { ($l, $r) = ($r, $l); }
  2         6  
12             my $sub = subname composition => sub {
13 5     5   40 $l->($r->(@_));
14 5         36 };
15 5         18 bless $sub, __PACKAGE__;
16             }
17              
18             sub backcompose {
19 1     1 0 889 my ($l, $r, $swap) = @_;
20              
21 1         5 compose($l, $r, !$swap);
22             }
23              
24             sub applyto {
25 2     2 0 848 my ($self, $other, $swap)=@_;
26              
27 2 50       9 if ($swap) {
28             # $other | $self
29 2         7 $self->($other);
30             } else {
31             # $self | $other
32 0           overload::Method($other, '|')->($other, $self, 1);
33             }
34             }
35              
36 10         73 use overload '<<' => \&compose,
37             '>>' => \&backcompose,
38             '|' => \&applyto,
39             # fallback is needed to avoid an error from Attribute::Handlers
40 10     10   6777 'fallback' => 1;
  10         19610  
41              
42             1;