FSM::Tiny - tiny implementation of finite state machine
This document describes FSM::Tiny version 0.03.
use FSM::Tiny; my $fsm = FSM::Tiny->new({ on_enter => sub { $_->{count} = 0; } }); $fsm->register(init => sub {}, [ add => sub { $_->{count} < 20 }, end => sub { $_->{count} >= 20 } ]); $fsm->register(add => sub { ++$_->{count} }, [ init => 1 ]); $fsm->register(end => sub { $_->{count} *= 5 }); $fsm->run; print $fsm->context->{count}; # => 100
This module is tiny implementation of finite state machine. this provides more simpler interface and code than any cpan's FSM::* modules.
current
define current state name for this machine.
rules
same as register function.
context
this is global variable of machine. in state behavior(as function) and guard function, it is read as $_
on_enter
it calls when machine transitions start.
on_transition
it calls in between transitions.
on_exit
it calls when machine transitions end.
new(%args)
you can define all rules and attributes in this initializer.
register($state_name => $state_fn, [%conditions])
registering state, state behavior(as function), and conditions for transition. %conditions is defined follows:
[ destination1 => sub { !!it_should_move_to_destination1_or_not() }, destination2 => sub { !!it_should_move_to_destination2_or_not() } ]
step
it run one transition.
run
it makes run until transitions end.
Perl 5.8.1 or later.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
perl
<Taiyoh Tanaka> <<sun.basix@gmail.com>>
Copyright (c) 2013, <Taiyoh Tanaka>. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install FSM::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm FSM::Tiny
CPAN shell
perl -MCPAN -e shell install FSM::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.