File Coverage

blib/lib/Bit/FlipFlop.pm
Criterion Covered Total %
statement 35 35 100.0
branch 13 14 92.8
condition 8 15 53.3
subroutine 11 11 100.0
pod 7 7 100.0
total 74 82 90.2


line stmt bran cond sub pod time code
1             package Bit::FlipFlop;
2              
3 1     1   8665 use strict;
  1         3  
  1         37  
4             #use warnings; # no warnings pragma - we'll try to be 5.005 compatible
5 1     1   4 use Carp;
  1         3  
  1         546  
6             $Bit::FlipFlop::VERSION = '0.01';
7              
8             # this will hold the flip flops' guts,
9             # keyed by stringified object references
10             %Bit::FlipFlop::stash = ();
11              
12             ########################################
13             # Public methods
14             #
15             # read the pod for usage
16             #
17             # the rest of this is an obsfucated black box. just kidding.
18              
19             sub new {
20 2     2 1 194 my $class = shift;
21 2         7 my %args = @_;
22              
23 2         3 my $ff=0;
24 2         5 my $f=bless \$ff, $class;
25              
26 2         12 $f->_init(%args);
27 2         5 return $f;
28             }
29              
30             sub test {
31 22     22 1 132 my $f = shift;
32              
33 22   66     67 $Bit::FlipFlop::stash{"$f"}{ff} ||= $f->_build_ff;
34              
35 22         22 $$f = &{$Bit::FlipFlop::stash{"$f"}{ff}};
  22         461  
36             }
37              
38             *Bit::FlipFlop::clock=\&test;
39              
40             sub state {
41 22 100   22 1 115 ${$_[0]}?1:0;
  22         45  
42             }
43              
44             sub series {
45 22     22 1 91 +${$_[0]};
  22         39  
46             }
47              
48             sub lead_edge {
49 22 100   22 1 92 ${$_[0]}==1?1:0;
  22         46  
50             }
51              
52             sub trail_edge {
53 38 100   38 1 93 ${$_[0]}=~/E/?1:0;
  38         132  
54             }
55              
56             sub next_test {
57 22 100 100 22 1 93 !${$_[0]}||$_[0]->trail_edge?'set':'reset';
58             }
59              
60              
61             ########################################
62             # Private methods
63              
64             sub _init {
65 2     2   4 my $f = shift;
66 2         5 my %args = @_;
67              
68 2 50 33     27 croak '"set" and "reset" coderef attributes are required.'
      33        
      33        
69             unless defined($args{set}) and ref($args{set}) eq 'CODE'
70             and defined($args{reset}) and ref($args{reset}) eq 'CODE';
71              
72 2 100       7 $args{simultaneous_edges} = 1
73             unless defined $args{simultaneous_edges};
74              
75 2         5 @{$Bit::FlipFlop::stash{$f}}{qw/set reset dotdot/} =
  2         19  
76             @args{qw/set reset simultaneous_edges/};
77             }
78              
79             sub _build_ff {
80 2     2   2 my $f = shift;
81              
82 2 100       8 my $op = $Bit::FlipFlop::stash{$f}{dotdot}?'..':'...';
83              
84 2         7 my $s = qq[
85             sub {
86             &{\$Bit::FlipFlop::stash{"$f"}{set}}
87             $op
88             &{\$Bit::FlipFlop::stash{"$f"}{reset}}
89             }
90             ];
91              
92             # debug:
93             # print $s;
94 2         180 eval $s;
95             }
96             1;
97             __END__