Bit-FlipFlop
view release on metacpan or search on metacpan
FlipFlop.pm view on Meta::CPAN
C<...> operator.
$result = set_condition() .. reset_condition();
Instead of subrefs, any Perl expression can be places on either side of
the operator. The expression on the left side is taken as the set
condition, that on the right as the reset condition.
If a flip flop with the attribute of C<simultaneous_edges=E<gt>1> is
desired, then use the C<..> operator. C<...> has the functionallity
of C<simultaneous_edges=E<gt>0>.
=item test
Under the B<SSBOM> interface, the flip flop tests its conditionals to see
if it needs to change state each time it is evaluated.
=item state
When a flip flop is evaluated, it return value, taken in a boolean
sense, represents the state of the flip flop. Should one wish to test
the state some time after the flip flop has been evaluated, then save
the return value into a scalar variable.
This OO example:
my $f = Bit::FlipFlop->new(set => sub { $_ == 3 },
reset => sub { $_ == 7 });
for (1..10) {
$f->test;
if ($f->state) {
++$count;
}
}
Under B<SSBOM> becomes:
for (1..10) {
if ($_ == 3 .. $_ == 7) {
++$count;
}
}
You may have noticed the dual use of the C<..> operator in that example.
This is an illusion. The characters C<..>, when evaluated in list
context is the normal range operator that you are used to.
Bit::FlipFlop's B<SSBOM> interface only applies to C<..> or C<...> in
scalar context.
=item series
To determine how many times a flip flop has been evaluated since being
in the true state, numerify its return value. This number includes the
evaluation that caused the flip flop to flip states to true. If the flip
flop is in the false state, then using this "method" will yield the
integer zero (0).
$r = set_test() ... reset_test();
# this same $r is present in the examples below
print 'the flip flop has been true for ', +$r, " iterations.\n";
=item lead_edge
The leading edge, when a flip flop changes from the false state to true
can be detected by testing the series for number 1.
print "leading edge\n" if $r == 1;
=item trail_edge
The trailing edge is when the flip flop changes from a state of true to
false. This is indicated in the B<SSBOM> interface by appending the string
'E0' to the return value of a flip flop's evaluation. Note that
when the return value is interpreted numerically or as a boolean, that
the presence or lacking of the 'E0' appendix is irrelevant.
print "trailing edge\n" if $r =~ /E/;
=item next_test
A flip flop will test the C<set> condition (left hand expression) when
the trailing edge has been crossed, or when its state is false.
print "will check left term next" if (not $r) or $r =~ /E/;
On the other hand, the flip flop will check the right hand expression
(the C<reset> conditional) if the state is true, and the trailing edge
has not yet been crossed.
print "will check right term next" if $r and $r !~ /E/;
=back
=head1 BACKGROUND
=head2 Digital Logic
Perl's flip flop operator has roots in digital logic circuitry.
___ $a $b | $o
$a -----| \ ---------|----
| |o---- $o F F | T
$b -----|___/ T F | T
F T | T
T T | F
The NAND gate. Its logical function could be expressed in Perl
as C<$o = not($a and $b);>. One diffence in the function of the NAND
gate and its Perl equivalent is that the NAND gate's output ceases
to retain its logical state when the input has been removed. It sure
would be nice to be able to maintain that bit of state.
_ ___ _ _ _
$s -----| \ $q $q $s $r | $q
| |o-+-- $q -----------------+-----
+-|___/ | F T T T | F
| / F T F T | T
| / T F T T | T
\__ /___ T F T F | F
( run in 1.191 second using v1.01-cache-2.11-cpan-96521ef73a4 )