FSA-Rules

 view release on metacpan or  search on metacpan

lib/FSA/Rules.pm  view on Meta::CPAN

    my $params = ref $_[0] ? shift : {};
    my $fsa = $machines{$self} = {
        done   => undef,
        notes  => {},
        stack  => [],
        table  => {},
        self   => $self,
    };

    # Weaken the circular reference.
    Scalar::Util::weaken $fsa->{self};

    $params->{state_class}  ||= 'FSA::State';
    $params->{state_params} ||= {};
    while (@_) {
        my $state = shift;
        my $def   = shift;
        $self->_croak(qq{The state "$state" already exists})
          if exists $fsa->{table}{$state};

        # Setup enter, exit, and do actions.

lib/FSA/Rules.pm  view on Meta::CPAN


        # Create the state object and cache the state data.
        my $obj = $params->{state_class}->new(%{$params->{state_params}});
        $def->{name} = $state;
        $def->{machine} = $self;
        $fsa->{table}{$state} = $obj;
        push @{$fsa->{ord}}, $obj;
        $states{$obj} = $def;

        # Weaken the circular reference.
        Scalar::Util::weaken $def->{machine};
    }

    # Setup rules. We process the table a second time to catch invalid
    # references.
    while (my ($key, $obj) = each %{$fsa->{table}}) {
        my $def = $states{$obj};
        if (my $rule_spec = $def->{rules}) {
            my @rules;
            while (@$rule_spec) {
                my $state = shift @$rule_spec;

lib/FSA/Rules.pm  view on Meta::CPAN

                # slower for code references.

                push @rules, {
                    state   => $fsa->{table}{$state},
                    rule    => $rule,
                    exec    => $exec,
                    message => $message,
                };

                # Weaken the circular reference.
                Scalar::Util::weaken $rules[-1]->{state};
            }
            $def->{rules} = \@rules;
        } else {
            $def->{rules} = [];
        }
    }

    # Handle any parameters.
    $self->start if $params->{start};
    $self->done($params->{done}) if exists $params->{done};



( run in 1.054 second using v1.01-cache-2.11-cpan-65fba6d93b7 )