File Coverage

blib/lib/Sub/Conveyor.pm
Criterion Covered Total %
statement 46 46 100.0
branch 9 10 90.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 4 4 100.0
total 70 73 95.8


line stmt bran cond sub pod time code
1             package Sub::Conveyor;
2              
3 3     3   423182 use 5.006;
  3         13  
4 3     3   21 use strict;
  3         17  
  3         94  
5 3     3   22 use warnings;
  3         7  
  3         1504  
6             our $VERSION = '0.03';
7              
8             sub new {
9 3     3 1 829536 my ($self) = shift;
10 3   33     32 $self = bless {
11             conveyor => [ ]
12             }, ref $self || $self;
13 3         16 while (@_) {
14 5         11 my ($type, $cb) = (shift, shift);
15 5         9 $self->add(@{$type}, $cb);
  5         13  
16             }
17 3         12 return $self;
18             }
19              
20             sub add {
21 11     11 1 91911 my ($self, @args) = @_;
22 11         23 my $cb = pop @args;
23 11         49 push @{$self->{conveyor}}, \@args, $cb;
  11         56  
24             }
25              
26             sub call {
27 11     11 1 50 my ($self, @params) = @_;
28 11         22 my $len = scalar @{ $self->{conveyor} };
  11         30  
29 11         71 for (my $i = 0; $i < $len; $i += 2) {
30 39         112 my $conveyor_count = scalar @{ $self->{conveyor}->[$i] };
  39         98  
31 39 100       104 if ( scalar @params == $conveyor_count ) {
32 36         61 my $match = 1;
33 36         85 for ( my $x = 0; $x < $conveyor_count; $x++ ) {
34 37         70 eval {
35 37         164 $self->{conveyor}->[$i]->[$x]->($params[$x])
36             };
37 37 100       29730 if ($@) {
38 17         144 $match = 0;
39             }
40             }
41 36 100       118 if ($match) {
42 19         77 @params = ($self->{conveyor}->[$i + 1]->(@params));
43             }
44             }
45             }
46 11 50       173 return wantarray ? @params : scalar @params == 1 ? $params[0] : \@params;
    100          
47             }
48              
49             sub install {
50 1     1 1 4 my ($self, $class, $method) = @_;
51 3     3   22 no strict 'refs';
  3         22  
  3         137  
52 3     3   16 no warnings 'redefine';
  3         5  
  3         508  
53 1     2   6 *{"${class}::${method}"} = sub { shift; $self->call(@_) };
  1         10  
  2         11  
  2         7  
54             }
55              
56             1;
57              
58             __END__