Bit-FlipFlop

 view release on metacpan or  search on metacpan

FlipFlop.pm  view on Meta::CPAN

* Note that in the default case, a flip flop tests the output of both
call backs during the call to C<test> that represents a leading 
edge.

=back

=head2 Super Stealth Built in Operator Mode

The functionallity that Bit::FlipFlop provides is so useful, that it can
be accessed just like a built in Perl operator. This is referred to as
"Super Stealth Built in Operator Mode", or B<SSBOM>. B<SSBOM> is the
preferred interface, and the author recommends only using the OO
interface as "training wheels".

One signals to the Bit::FlipFlop module that the B<SSBOM> interface is 
desired by commenting out the C<use> statement:

  #use Bit::FlipFlop;

B<Advantage of SSBOM>

Why should you go through the trouble of learning this slightly cryptic
B<SSBOM> interface? Well, for one reason is that the Bit::FlipFlop
module *need not be installed* on the target system. This alone is a
huge advantage. Another reason is that the B<SSBOM> interface has been
standard for many years, and is well known by *thousands* of Perl
programmers world wide. B<SSBOM> is more readily available, more well
known and thus lends to maintainable code.

Of course, since Bit::FlipFlop objects and their methods won't be
available, coding style must be adjusted. What follows are code excerpts
to accomplish the same things that the OO method calls do.  

=over 4

=item new

FlipFlop.pm  view on Meta::CPAN

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).

FlipFlop.pm  view on Meta::CPAN

=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.



( run in 0.374 second using v1.01-cache-2.11-cpan-e9daa2b36ef )