Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday December 13, 2004
09:31 PM

DFA Build Process

[ #22298 ]

The build process for the next version of Bricolage was becoming too complicated. Because we want it to be considerably easier to install, we're putting a lot of effort into ensuring that everything "just works." My code was getting too complex handling all of the rules so we're making the switch to a DFA engine and simply describing what to do when particular conditions occur. Unfortunately, DFA::Simple has a rather confusing interface and even after reading the Perl.com article about it I didn't quite "get" it. That led me to rewrite the interface to something I can actually use. Here's how to play ping-pong with a state machine:

my ($goto, $count) = ('', 0);
my %state_machine = (
    ping => {
        enter => sub { $goto = 'pong' },
        leave => sub { warn "leaving state 'ping'" },
        goto  => [
            # goto         when             and do this
            pong => [sub {'pong' eq $goto}, sub {$count++}],
        ],
    },
    pong => {
        enter => sub { $goto = 'ping' },
        leave => sub { warn "leaving state 'pong'" },
        goto  => [
            ping => [sub {'ping' eq $goto}, sub {$count++}],
        ]
    }
);
my $dfa = Bric::Util::DFA->new(
    \%state_machine,
    'ping', # state to start in
    sub {$count >= 20} # done when true
);
$dfa->next_state while ! $dfa->done;

Yeah, it needs some work, but it's much easier for me to understand.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.