File Coverage

blib/lib/Trigger.pm
Criterion Covered Total %
statement 50 52 96.1
branch 13 18 72.2
condition 1 3 33.3
subroutine 6 7 85.7
pod 2 2 100.0
total 72 82 87.8


line stmt bran cond sub pod time code
1             package Trigger;
2              
3 3     3   1480 use strict;
  3         7  
  3         110  
4 3     3   45 use 5.8.1;
  3         8  
  3         188  
5             our $VERSION = '0.02';
6              
7 3     3   15 use base qw(Class::Accessor::Fast);
  3         4  
  3         3822  
8              
9             sub new {
10 2     2 1 80 my $class = shift;
11 2         29 my $self = $class->SUPER::new();
12 2         35 $self->mk_accessors(qw/heap _process _trigger _action _trigger_and_action _end/);
13            
14 2         553 my $args = {
15             #inline_states => {},
16             @_
17             };
18              
19 2 100       13 if( my $inline_states = delete $args->{inline_states} ){
20             $args = {
21             heap => {},
22             init => undef,
23 0     0   0 process => sub { 1 },
24 1         21 trigger_and_action => [],
25             end => undef,
26 1         6 %{$inline_states}
27             };
28             }
29              
30 2 50       21 $self->heap($args->{heap}) if defined $args->{heap};
31 2         27 $self->_process($args->{process});
32 2         15 $self->_trigger([]);
33 2         12 $self->_action([]);
34 2         27 $self->_end($args->{end});
35 2         11 $self->_trigger_and_action( scalar @{$args->{trigger_and_action}} );
  2         11  
36 2 50       12 $self->_trigger_and_action % 2 and die ;
37 2         16 while( @{$args->{trigger_and_action}} ){
  7         41  
38 5         8 my($trigger, $action) = splice @{$args->{trigger_and_action}}, 0, 2;
  5         15  
39 5         9 push @{$self->_trigger}, $trigger;
  5         15  
40 5         20 push @{$self->_action} , $action;
  5         14  
41             }
42 2         7 $self->_trigger_and_action( $self->_trigger_and_action / 2 - 1);
43 2 50 33     49 $args->{init}->($self) if defined $args->{init} and ref $args->{init};
44 2         16 return $self;
45             }
46              
47             sub eval{
48 4     4 1 134 my $self = shift;
49 4         5 my @action_re;
50 4         14 my @process_re = $self->_process->($self => @_);
51 4         68 for ( 0 .. $self->_trigger_and_action ){
52 12 100       193 if( my @trigger_re = $self->_trigger->[$_]->($self => @process_re) ){
53 3 100       59 if( ref $self->_action->[$_] eq 'CODE' ){
    50          
54 2         16 @action_re = $self->_action->[$_]->($self => @trigger_re) ;
55             }elsif( ref $self->_action->[$_] eq 'ARRAY' ){
56 1         17 for my $code( @{$self->_action->[$_]} ){
  1         13  
57 2         29 @action_re = $code->($self => @trigger_re);
58             }
59             }else{
60 0         0 die ref $self->_action->[$_];
61             }
62             }
63             }
64 4 100       86 @action_re ? return @action_re : return @process_re;
65             }
66              
67             sub DESTROY{
68 2     2   18 my $self = shift;
69 2 50       10 $self->_end->($self) if $self->_end;
70 2         18 delete $self->{$_} for (keys %{$self});
  2         260  
71             }
72              
73             1;
74             __END__