blib/lib/FSM/Basic.pm

filter: Statementstmt 53.0% 155 / 292 Branchbran 42.6% 52 / 122 Conditioncond 29.1% 23 / 79 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 72.2% 13 / 18 Podpod 11.1% 1 / 9 totaltotal 43.6% 244 / 559 CC 110 SCAR 66.9
CC
110
Cov
47%
run
1211.3
alter
30.0
set
20.0
CRAP 804.5
↑ Summary
1 package FSM::Basic;
2  
3 7 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our $VERSION = '0.28';
12  
13 6 ▶sub new {
Subroutine new: called CC=7 SCAR=21.1
Pod documented
14 6 my ($class, $l, $s) = @_;
15 6 my $self;
16 6 $self->{states_list} = $l;
17 6 $self->{state} = $s;
18 6 foreach my $k1 (keys %{ $self->{states_list} }) {
19 18 ▶ if (exists $self->{states_list}{$k1}{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$k1}{"expect"}) 12 6 18
20 12 foreach my $k2 (keys %{ $self->{states_list}{$k1}{expect} }) {
21 33 ▶ if (ref $self->{states_list}{$k1}{expect}{$k2} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" and exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}) 0 33 33
Condition66%2/3
ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" and exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
!ll&&!rl&&r
6270
MC/DC0%0/2
ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" and exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
Truth table66%2/3
A and B
A= ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH"B= exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
ABresult
0X0
100
111
22 && exists $self->{states_list}{$k1}{expect}{$k2}{alternation})
23 {
24 0 ▶ if (defined $self->{states_list}{$k1}{expect}{$k2}{caseinsensitive}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'states_list'}{$k1}{'expect'}{$k2}{'caseinsensitive'}) { } 0 0 0
25 0 $self->{states_list}{$k1}{expect}{ alter($k2, 1) } = delete $self->{states_list}{$k1}{expect}{$k2};
26 } else {
27 0 $self->{states_list}{$k1}{expect}{ alter($k2) } = delete $self->{states_list}{$k1}{expect}{$k2};
28 }
29 }
30 }
31 }
32 }
33 6 bless($self, $class);
34 6 return $self;
35 }
36  
37 7 my @commands = qw(
38 cmd
39 cmd_exec
40 exec
41 cmd_regex
42 do
43 output
44 output_file
45 cat
46 more
47 catrand
48 catwrand
49 catseq
50 catseq_idx
51 catseqn
52 swapregex
53 matching
54 final
55 matching
56 caseinsensitive
57 alternation
58 );
59  
60  
61 0 ▶sub qp(&){
Subroutine qp: not called CC=3 SCAR=24.8
Pod undocumented
62 0 my $s;
63 0 ▶ open local *STDOUT, '>', \$s or die "open in-memory file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open local *STDOUT, ">", \$s or die "open in-memory file: $!" 0 0 0
64 0 &{$_[0]};
65 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
66 0 $s;
67 }
68  
69 40018 ▶sub run {
Subroutine run: called CC=84 SCAR=71.0
Pod undocumented
70 40018 my ($self, $in) = @_;
71 40018 my $in_lc = lc($in);
72 40018 $in =~ s/([*?.])/\\$1/g;
73 40018 my $rev;
74 my %extra;
75 40018 ▶ if ($in !~ /^$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in =~ /^$/) 40018 0 40018
76 40018 foreach my $IN (grep { /^$in/i } keys %{ $self->{states_list}{ $self->{state} }{expect} }) {
77 40018 ▶ if (ref $self->{states_list}{ $self->{state} }{expect}{$IN} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN} eq "HASH") 40018 0 40018
78 40018 ▶ $rev //= $IN if (defined $self->{states_list}{ $self->{state} }{expect}{$IN}{swapregex});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN}{"swapregex"} 0 40018 40018
Condition0%0/3
$rev //= $IN
l!l&&r!l&&!r
000
MC/DC0%0/2
$rev //= $IN
$rev $IN
Truth table0%0/3
A //= B
A= $revB= $IN
ABresult
1X1
011
000
79 40018 foreach my $key (keys %{ $self->{states_list}{ $self->{state} }{expect}{$IN} }) {
80 # carp "k=$key IN=$IN";
81 80047 ▶ $extra{$key} = $self->{states_list}{ $self->{state} }{expect}{$IN}{$key} unless any($key);
Branch: 1 branch
BranchTrueFalseTotal
unless any($key) 8 80039 80047
82 }
83 }
84 }
85 }
86 # carp "state=".Dumper($self->{states_list});
87 40018 my $output = '';
88 40018 my $string;
89 40018 ▶ if (exists $self->{states_list}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}) 40018 0 40018
90 40018 ▶ if ( exists $self->{states_list}{ $self->{state} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"} and $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0) 0 40018 40018
Condition66%4/6
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"} and $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
!ll&&!rl&&r
4001260
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"}
!ll&&!rl&&r
0400126
MC/DC0%0/3
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"} and $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
exists $self->{"states_list"}{$self->{"state"}} exists $self->{"states_list"}{$self->{"state"}}{"repeat"} $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
Truth table50%2/4
A and B and C
A= exists $self->{"states_list"}{$self->{"state"}}B= exists $self->{"states_list"}{$self->{"state"}}{"repeat"}C= $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
ABCresult
0XX0
10X0
1100
1111
91 && exists $self->{states_list}{ $self->{state} }{repeat}
92 && $self->{states_list}{ $self->{state} }{repeat} <= 0)
93 {
94 0 $self->{previous_state} = $self->{state};
95 0 ▶ $self->{state} = $self->{states_list}{ $self->{state} }{expect}{not_matching0} // $self->{states_list}{ $self->{state} }{not_matching0};
Condition0%0/3
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} // $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} // $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
Truth table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"}B= $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
ABresult
1X1
011
000
96 0 ▶ if (exists $self->{states_list}{ $self->{previous_state} }{not_matching_info_last}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"previous_state"}}{"not_matching_info_last"}) 0 0 0
97 0 $output = $self->{states_list}{ $self->{previous_state} }{not_matching_info_last};
98 }
99 0 ▶ $output .= $self->{states_list}{ $self->{state} }{output} // read_file($self->{states_list}{ $self->{state} }{output_file}) // '';
Condition0%0/5
($self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
l!l
00
$self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
$self->{"states_list"}{$self->{"state"}}{"output"} read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
Truth table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"output"}B= read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
ABresult
1X1
011
000
100 0 ▶ return ($self->{states_list}{ $self->{state} }{final} // 0, $output);
Condition0%0/2
$self->{"states_list"}{$self->{"state"}}{"final"} // 0
l!l
00
MC/DC0%0/1
$self->{"states_list"}{$self->{"state"}}{"final"} // 0
$self->{"states_list"}{$self->{"state"}}{"final"}
Truth table0%0/2
A
A= $self->{"states_list"}{$self->{"state"}}{"final"}
Aresult
11
00
101 }
102 40018 ▶ if (exists $self->{states_list}{ $self->{state} }{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"expect"}) 40018 0 40018
103 40018 ▶ if (exists $self->{states_list}{ $self->{state} }{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"info"}) 0 40018 40018
104 0 $output = $self->{states_list}{ $self->{state} }{info} . $output;
105 }
106 40018 ▶ if (exists $self->{states_list}{ $self->{state} }{info_once}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"info_once"}) 0 40018 40018
107 0 $output = delete($self->{states_list}{ $self->{state} }{info_once}) . $output;
108 }
109 40018 my $state;
110 40018 ▶ if ( exists $self->{previous_output}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/) 0 40018 40018
Condition50%3/6
exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/
!ll&&!rl&&r
4001800
exists $self->{"previous_output"} and $in eq ""
!ll&&!rl&&r
6400120
MC/DC0%0/3
exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/
exists $self->{"previous_output"} $in eq "" $self->{"previous_output"} =~ /\[(.+)\]/
Truth table50%2/4
A and B and C
A= exists $self->{"previous_output"}B= $in eq ""C= $self->{"previous_output"} =~ /\[(.+)\]/
ABCresult
0XX0
10X0
1100
1111
111 && $in eq ''
112 && $self->{previous_output} =~ /\[(.+)\]/)
113 {
114 0 $in = $1;
115 }
116 40018 my $match;
117 40018 ▶ if (defined $rev) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $rev) { } 0 40018 40018
elsif (exists $self->{'states_list'}{$self->{'state'}}{'expect'}{$in_lc} and defined $self->{'states_list'}{$self->{'state'}}{'expect'}{$in_lc}{'caseinsensitive'}) { } 0 40018 40018
elsif (exists $self->{'states_list'}{$self->{'state'}}{'expect'}{$in}) { } 40018 0 40018
118 0 my $key = $rev;
119 0 my $r = $in;
120 0 ▶ $r = '(?i:' . $r . ')' if defined $self->{states_list}{ $self->{state} }{expect}{$rev}{caseinsensitive};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$rev}{"caseinsensitive"} 0 0 0
121 0 ▶ if ($r && $key =~ /^$r/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r and $key =~ /^$r/) 0 0 0
Condition0%0/3
$r and $key =~ /^$r/
!ll&&!rl&&r
000
MC/DC0%0/2
$r and $key =~ /^$r/
$r $key =~ /^$r/
Truth table0%0/3
A and B
A= $rB= $key =~ /^$r/
ABresult
0X0
100
111
122 0 ▶ if (@+) {
Branch: 1 branch
BranchTrueFalseTotal
if (@+) 0 0 0
123 0 for my $nbr (1 .. (scalar(@+) - 1)) {
124 0 ▶ if (defined $-[$nbr]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $-[$nbr]) 0 0 0
125 0 $match = substr($key, $-[$nbr], $+[$nbr] - $-[$nbr]);
126 0 ▶ if (defined($+[$nbr])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+[$nbr]) 0 0 0
127 0 $self->{cmd_regex}{$nbr} = substr($key, $-[$nbr], $+[$nbr] - $-[$nbr]);
128 }
129 }
130 }
131 }
132 0 $state = $self->{states_list}{ $self->{state} }{expect}{$key};
133 }
134 ▶ } elsif (exists $self->{states_list}{ $self->{state} }{expect}{$in_lc}
Condition33%1/3
exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc} and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
!ll&&!rl&&r
0400180
MC/DC0%0/2
exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc} and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc} defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
Truth table33%1/3
A and B
A= exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}B= defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
ABresult
0X0
100
111
135 && defined $self->{states_list}{ $self->{state} }{expect}{$in_lc}{caseinsensitive})
136 {
137 0 $state = $self->{states_list}{ $self->{state} }{expect}{$in_lc};
138 } elsif (exists $self->{states_list}{ $self->{state} }{expect}{$in}) {
139 40018 $state = $self->{states_list}{ $self->{state} }{expect}{$in};
140 } else {
141 0 foreach my $key (keys %{ $self->{states_list}{ $self->{state} }{expect} }) {
142 0 ▶ if ($in =~ /^$key$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^$key$/) 0 0 0
143 0 ▶ if (@+) {
Branch: 1 branch
BranchTrueFalseTotal
if (@+) 0 0 0
144 0 for my $nbr (1 .. (scalar(@+) - 1)) {
145 0 ▶ if (defined $-[$nbr]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $-[$nbr]) 0 0 0
146 0 $match = substr($in, $-[$nbr], $+[$nbr] - $-[$nbr]);
147 0 ▶ if (defined($+[$nbr])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+[$nbr]) 0 0 0
148 0 $self->{cmd_regex}{$nbr} = substr($in, $-[$nbr], $+[$nbr] - $-[$nbr]);
149 }
150 }
151 }
152 }
153 0 $state = $self->{states_list}{ $self->{state} }{expect}{$key};
154 }
155 }
156 }
157 40018 ▶ if (ref $state eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state eq 'HASH') { } 40018 0 40018
158 40018 $self->{previous_state} = $self->{state};
159 40018 ▶ $self->{previous_output} = $state->{output} // $self->{states_list}{ $self->{state} }{output} // read_file($self->{states_list}{ $self->{state} }{output_file}) // '';
Condition37%3/8
(($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
l!l
400180
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
l!l&&r!l&&!r
4001800
$state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}
l!l&&r!l&&!r
0400180
MC/DC0%0/3
(($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
$state->{"output"} $self->{"states_list"}{$self->{"state"}}{"output"} read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
Truth table25%1/4
A // B // C
A= $state->{"output"}B= $self->{"states_list"}{$self->{"state"}}{"output"}C= read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
ABCresult
1XX1
01X1
0011
0000
160 40018 ▶ $self->{state} = $state->{matching} // $self->{state};
Condition66%2/3
$state->{"matching"} // $self->{"state"}
l!l&&r!l&&!r
6400120
MC/DC0%0/2
$state->{"matching"} // $self->{"state"}
$state->{"matching"} $self->{"state"}
Truth table66%2/3
A // B
A= $state->{"matching"}B= $self->{"state"}
ABresult
1X1
011
000
161 40018 ▶ $output .= $state->{output} // $self->{states_list}{ $self->{state} }{output} // read_file($self->{states_list}{ $self->{state} }{output_file}) // '';
Condition37%3/8
(($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
l!l
400180
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
l!l&&r!l&&!r
4001800
$state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}
l!l&&r!l&&!r
0400180
MC/DC0%0/3
(($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
$state->{"output"} $self->{"states_list"}{$self->{"state"}}{"output"} read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
Truth table25%1/4
A // B // C
A= $state->{"output"}B= $self->{"states_list"}{$self->{"state"}}{"output"}C= read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
ABCresult
1XX1
01X1
0011
0000
162 40018 ▶ if (exists $state->{cmd}) {
Branch: 11 branches
BranchTrueFalseTotal
if (exists $state->{'cmd'}) { } 0 40018 40018
elsif (exists $state->{'cmd_exec'}) { } 0 40018 40018
elsif (exists $state->{'exec'}) { } 0 40018 40018
elsif (exists $state->{'do'}) { } 0 40018 40018
elsif (exists $state->{'output_file'}) { } 0 40018 40018
elsif (exists $state->{'cat'}) { } 1 40017 40018
elsif (exists $state->{'more'}) { } 0 40017 40017
elsif (exists $state->{'catRAND'}) { } 20000 20017 40017
elsif (exists $state->{'catWRAND'}) { } 20000 17 20017
elsif (exists $state->{'catSEQ'}) { } 3 14 17
elsif (exists $state->{'catSEQn'}) { } 8 6 14
163 0 my $cmd_state = delete $state->{cmd};
164 0 $cmd_state =~ s/\$in/$in/g;
165 0 push(@{ $self->{cmd_stack} }, $cmd_state);
166 } elsif (exists $state->{cmd_exec}) {
167 0 my $cmd_exec = join ' ', @{ $self->{cmd_stack} };
168 0 $string = `$cmd_exec`;
169 0 $output = sprintf("%s", $string) . $output;
170 0 $self->{cmd_exec} = [];
171 } elsif (exists $state->{exec}) {
172 0 my $old_exec = $state->{exec};
173 0 $state->{exec} =~ s/__IN__/$in/g;
174 0 foreach my $k (keys %{ $self->{cmd_regex} }) {
175 0 my $v = $self->{cmd_regex}{$k};
176 0 my $K = '__' . $k . '__';
177 0 $state->{exec} =~ s/$K/$v/g;
178 }
179 0 $string = `$state->{exec}`;
180 0 $output = sprintf("%s", $string) . $output;
181 0 $state->{exec} = $old_exec;
182 } elsif (exists $state->{do}) {
183 0 my $old_do = $state->{do};
184 0 $state->{do} =~ s/__IN__/$in/g;
185 0 foreach my $k (keys %{ $self->{cmd_regex} }) {
186 0 my $v = $self->{cmd_regex}{$k};
187 0 my $K = '__' . $k . '__';
188 0 $state->{do} =~ s/$K/$v/g;
189 }
190  
191 0 ▶ my $res = qp { eval ($state->{do}) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 $output = $res . $output;
193 0 $state->{do} = $old_do;
194 } elsif (exists $state->{output_file}) {
195 0 $output = read_file($state->{output_file});
196 0 chomp $output;
197 } elsif (exists $state->{cat}) {
198 1 my $old_cat = $state->{cat};
199 1 $state->{cat} =~ s/__IN__/$in/g;
200 1 foreach my $k (keys %{ $self->{cmd_regex} }) {
201 0 my $v = $self->{cmd_regex}{$k};
202 0 my $K = '__' . $k . '__';
203 0 $state->{cat} =~ s/$K/$v/g;
204 }
205 1 $string = read_file($state->{cat});
206 1 $output = sprintf("%s", $string) . $output;
207 1 $state->{cat} = $old_cat;
208 } elsif (exists $state->{more}) {
209 0 my $old_cat = $state->{more};
210 0 $state->{more} =~ s/__IN__/$in/g;
211 0 foreach my $k (keys %{ $self->{cmd_regex} }) {
212 0 my $v = $self->{cmd_regex}{$k};
213 0 my $K = '__' . $k . '__';
214 0 $state->{more} =~ s/$K/$v/g;
215 }
216 0 $string = read_file($state->{more});
217 0 $extra{more} = 1;
218 0 $output = sprintf("%s", $string) . $output;
219 0 $state->{more} = $old_cat;
220 } elsif (exists $state->{catRAND}) {
221 20000 my $old_cat = $state->{catRAND};
222 20000 $state->{catRAND} =~ s/__IN__/$in/g;
223 20000 foreach my $k (keys %{ $self->{cmd_regex} }) {
224 0 my $v = $self->{cmd_regex}{$k};
225 0 my $K = '__' . $k . '__';
226 0 $state->{catRAND} =~ s/$K/$v/g;
227 }
228 20000 my @files = split /\s+/, $state->{catRAND};
229 20000 my $file = $files[ rand @files ];
230 20000 $string = read_file($file);
231 20000 $output = sprintf("%s", $string) . $output;
232 20000 $state->{catRAND} = $old_cat;
233 } elsif (exists $state->{catWRAND}) {
234 20000 my $old_cat = $state->{catWRAND};
235 20000 $state->{catWRAND} =~ s/__IN__/$in/g;
236 20000 foreach my $k (keys %{ $self->{cmd_regex} }) {
237 0 my $v = $self->{cmd_regex}{$k};
238 0 my $K = '__' . $k . '__';
239 0 $state->{catWRAND} =~ s/$K/$v/g;
240 }
241 20000 my %files = map { split /:/ } split /\s+/, $state->{catWRAND};
242 20000 my $file;
243 my $weight;
244 20000 ▶ while (my ($p, $w) = each %files) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %files) 40000 20000 60000
245 40000 ▶ $w //= 1;
Condition50%1/2
$w //= 1
l!l
400000
MC/DC0%0/1
$w //= 1
$w
Truth table50%1/2
A
A= $w
Aresult
11
00
246 40000 ▶ $weight += $w // 1;
Condition50%1/2
$w // 1
l!l
400000
MC/DC0%0/1
$w // 1
$w
Truth table50%1/2
A
A= $w
Aresult
11
00
247 40000 ▶ $file = $p if rand($weight) < $w;
Branch: 1 branch
BranchTrueFalseTotal
if rand $weight < $w 30035 9965 40000
248 }
249 20000 $string = read_file($file);
250 20000 $output = sprintf("%s", $string) . $output;
251 20000 $state->{catWRAND} = $old_cat;
252 } elsif (exists $state->{catSEQ}) {
253 3 my $old_cat = $state->{catSEQ};
254 3 my $state_file;
255 3 ▶ if (exists $state->{catSEQ_idx}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'catSEQ_idx'}) { } 3 0 3
256 3 $state_file = $state->{catSEQ_idx};
257 } else {
258 0 $state_file = $old_cat . '.state';
259 0 $state_file =~ s/\s/_/g;
260 }
261 3 $state->{catSEQ} =~ s/__IN__/$in/g;
262 3 foreach my $k (keys %{ $self->{cmd_regex} }) {
263 0 my $v = $self->{cmd_regex}{$k};
264 0 my $K = '__' . $k . '__';
265 0 $state->{catSEQ} =~ s/$K/$v/g;
266 }
267 3 my @files = split /\s+/, $state->{catSEQ};
268 3 tie my $nbr => 'FSM::Basic::Modulo', scalar @files, 0;
269 3 ▶ if (-f $state_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $state_file) 2 1 3
270 2 $nbr = read_file($state_file);
271 }
272 3 my $file = $files[ $nbr++ ];
273 3 $string = read_file($file);
274 3 $output = sprintf("%s", $string) . $output;
275 3 $state->{catSEQ} = $old_cat;
276 3 write_file($state_file, $nbr);
277 } elsif (exists $state->{catSEQn}) {
278 8 my $old_cat = $state->{catSEQn};
279 8 my @files_n = split /\s+/, $state->{catSEQn};
280 8 my @files;
281 8 foreach my $f_n (@files_n) {
282 16 my ($i, $f) = split ',', $f_n;
283 16 for (1 .. $i) {
284 56 push @files, $f;
285 }
286 }
287 8 my $state_name = $files[0];
288 8 my $state_file;
289 8 ▶ if (exists $state->{catSEQn_idx}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'catSEQn_idx'}) { } 8 0 8
290 8 $state_file = $state->{catSEQn_idx};
291 } else {
292 0 $state_file = $state_name . '.state';
293 0 $state_file =~ s/\s/_/g;
294 }
295 8 $state->{catSEQn} =~ s/__IN__/$in/g;
296 8 foreach my $k (keys %{ $self->{cmd_regex} }) {
297 0 my $v = $self->{cmd_regex}{$k};
298 0 my $K = '__' . $k . '__';
299 0 $state->{catSEQn} =~ s/$K/$v/g;
300 }
301 8 tie my $nbr => 'FSM::Basic::Modulo', scalar @files, 0;
302 8 ▶ if (-f $state_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $state_file) 7 1 8
303 7 $nbr = read_file($state_file);
304 }
305 8 my $file = $files[ $nbr++ ];
306 8 $string = read_file($file);
307 8 $output = sprintf("%s", $string) . $output;
308 8 $state->{catSEQn} = $old_cat;
309 8 write_file($state_file, $nbr);
310 }
311 } else {
312 0 $self->{previous_state} = $self->{state};
313 0 ▶ $self->{state} = $self->{states_list}{ $self->{state} }{not_matching} // $self->{state};
Condition0%0/3
$self->{"states_list"}{$self->{"state"}}{"not_matching"} // $self->{"state"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"states_list"}{$self->{"state"}}{"not_matching"} // $self->{"state"}
$self->{"states_list"}{$self->{"state"}}{"not_matching"} $self->{"state"}
Truth table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"not_matching"}B= $self->{"state"}
ABresult
1X1
011
000
314 0 ▶ $self->{states_list}{ $self->{state} }{repeat}-- if exists $self->{states_list}{ $self->{state} }{repeat};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"states_list"}{$self->{"state"}}{"repeat"} 0 0 0
315 0 ▶ $output .= $self->{states_list}{ $self->{state} }{output} // read_file($self->{states_list}{ $self->{state} }{output_file}) // '';
Condition0%0/5
($self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
l!l
00
$self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
$self->{"states_list"}{$self->{"state"}}{"output"} read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
Truth table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"output"}B= read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
ABresult
1X1
011
000
316 0 ▶ if (exists $self->{states_list}{ $self->{state} }{not_matching_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"not_matching_info"}) 0 0 0
317 0 $output = $self->{states_list}{ $self->{state} }{not_matching_info} . "\n" . $output;
318 }
319 #return ($self->{states_list}{ $self->{state} }{$in}{final} // $self->{states_list}{ $self->{state} }{final} // 0, $output, \%extra);
320 }
321 40018 ▶ return ($self->{states_list}{ $self->{state} }{$in}{final} // $self->{states_list}{ $self->{state} }{final} // 0, $output, \%extra);
Condition40%2/5
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
l!l
040018
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}
l!l&&r!l&&!r
0040018
MC/DC0%0/2
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} $self->{"states_list"}{$self->{"state"}}{"final"}
Truth table33%1/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{$in}{"final"}B= $self->{"states_list"}{$self->{"state"}}{"final"}
ABresult
1X1
011
000
322 }
323 }
324 }
325  
326 80047 ▶sub any {
Subroutine any: called CC=3 SCAR=11.0
Pod undocumented
327 80047 my ($in) = @_;
328 80047 my $out = 0;
329 80047 foreach (@commands) {
330 1600940 ▶ $out = 1 if (lc($in) eq $_);
Branch: 1 branch
BranchTrueFalseTotal
if lc $in eq $_ 80045 1520895 1600940
331 }
332 80047 return $out;
333 }
334  
335 0 ▶sub set {
Subroutine set: not called CC=4 SCAR=30.0
Pod undocumented
336 0 my ($self, $in) = @_;
337 0 $self->{previous_state} = $self->{state};
338 0 ▶ $self->{previous_output} = $self->{states_list}{ $self->{state} }{output} // read_file($self->{states_list}{ $self->{state} }{output_file}) // '';
Condition0%0/5
($self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
l!l
00
$self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->{"states_list"}{$self->{"state"}}{"output"} // read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})) // ""
$self->{"states_list"}{$self->{"state"}}{"output"} read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
Truth table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"output"}B= read_file($self->{"states_list"}{$self->{"state"}}{"output_file"})
ABresult
1X1
011
000
339 0 ▶ $self->{state} = $in if exists $self->{states_list}{$in};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"states_list"}{$in} 0 0 0
340 0 return 1;
341 }
342  
343 11 ▶sub write_file {
Subroutine write_file: called CC=3 SCAR=11.3
Pod undocumented
344 11 my ($file, $content) = @_;
345 11 ▶ open my $fh, '>', $file or die "Error opening file for write $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Error opening file for write ${file}: $!\n" 11 0 11
346 11 print $fh $content;
347 11 ▶ close $fh or die "Error closing file $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Error closing file ${file}: $!\n" 11 0 11
348 11 return 1;
349 }
350  
351 0 ▶sub alter {
Subroutine alter: not called CC=5 SCAR=34.0
Pod undocumented
352 0 my $r = shift;
353 0 ▶ my $c = shift // 0;
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
354 0 $r =~ /([^[]*)\[([^\]]+)\](.*)/;
355 0 my $pre_r = $1;
356 0 my $match_r = $2;
357 0 my $post_r = $3;
358 0 my $out = $pre_r . '(';
359 0 ▶ for (my $i = 1 ; $i <= length $match_r ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= length $match_r) 0 0 0
360 0 $out .= substr($match_r, 0, $i) . '|';
361 }
362 0 chop $out;
363 0 ▶ $post_r = alter($post_r) if $post_r =~ /\[([^\]]+)\]/;
Branch: 1 branch
BranchTrueFalseTotal
if $post_r =~ /\[([^\]]+)\]/ 0 0 0
364 0 $out = $out . ')' . $post_r;
365 0 ▶ $out = '(?i:' . $out . ')' if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
366 0 return $out;
367 }
368  
369 40021 ▶sub read_file {
Subroutine read_file: called CC=5 SCAR=17.1
Pod undocumented
370 40021 my ($file) = @_;
371 40021 my $content = '';
372 40021 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 40021 0 40021
373 40021 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 40021 0 40021
374 40021 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Error opening file for read ${file}: $!\n" 40021 0 40021
375 or die "Error opening file for read $file: $!\n";
376 40021 local $/;
377 40021 $content = <$fh>;
378 40021 ▶ close $fh or die "Error closing file $file: $!\n";;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Error closing file ${file}: $!\n" 40021 0 40021
379 } else {
380 0 carp "missing file: $file";
381 }
382 }
383 40021 return $content;
384 }
385  
386 0 ▶sub rotate_files {
Subroutine rotate_files: not called CC=3 SCAR=24.8
Pod undocumented
387 0 my @all = @_;
388 0 my $uuid = do {
389 0 ▶ open my $fh, "/proc/sys/kernel/random/uuid"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "/proc/sys/kernel/random/uuid" or die $! 0 0 0
390 or die $!;
391 0 scalar <$fh>;
392 };
393 0 chomp $uuid;
394 0 rename($all[0], $all[0] . '.' . $uuid);
395 0 ▶ for (my $idx = 1 ; $idx < $#all + 1 ; $idx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $#all + 1) 0 0 0
396 0 rename($all[$idx], $all[ $idx - 1 ]);
397 }
398 0 rename($all[0] . '.' . $uuid, $all[-1]);
399 }
400  
401  
402 package FSM::Basic::Modulo;
403 11 ▶sub TIESCALAR { bless [ $_[2] || 0, $_[1] ] => $_[0] }
Subroutine TIESCALAR: called CC=2 SCAR=7.6
Condition50%1/2
$_[2] || 0
l!l
011
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
404 22 ▶sub FETCH { ${ $_[0] }[0] }
Subroutine FETCH: called CC=1 SCAR=0.0
405 20 ▶sub STORE { ${ $_[0] }[0] = $_[1] % ${ $_[0] }[1] }
Subroutine STORE: called CC=1 SCAR=0.0
406 7 1;
407  
408 =head1 NAME
409  
410 FSM::Basic - Finite state machine using HASH as state definitions
411  
412 =head1 VERSION
413  
414 Version 0.25
415  
416 =cut
417  
418 =head1 SYNOPSIS
419  
420 A small finite state machine using a HASH data as description of the states
421 Mainly used to create fake bash or fake telnet server in the purpose to mimic some CLI device interface (like SWITCH or ROUTER interface)
422 Perhaps a little code snippet.
423 The HASH is easily using a JSON file
424  
425 use FSM::Basic;
426  
427 my $fsm = FSM::Basic->new( \%states, 'accept' );
428 my $final = 0;
429 my $out;
430 my $extra;
431 foreach my $in ( @ins )
432 {
433 ( $final, $out, $extra) = $fsm->run( $in );
434 say $out;
435 last if $final;
436 }
437  
438  
439 =head1 SUBROUTINES/METHODS
440  
441 =head2 new
442  
443 my $fsm = FSM::Basic->new( \%states, 'accept' );
444  
445 Create the FSM with the HASH ref as first paramter
446 and the initial state as second parameter
447  
448 The HASH is like this:
449  
450 my %states = (
451 'accept' => {
452 'expect' => {
453 'default' => {
454 'final' => 0,
455 'matching' => 'prompt'
456 }
457 },
458 'not_matching' => 'accept',
459 'not_matching0' => 'close',
460 'not_matching_info_last' => '% Bad passwords
461 ',
462 'output' => 'Password: ',
463 'repeat' => 2
464 },
465  
466 'close' => {'final' => 1},
467 'prompt' => {
468 'expect' => {
469 'not_matching' => 'prompt',
470 'exit' => {
471 'matching' => 'close',
472 'final' => 0
473 },
474 'meminfo' => {'do' => 'do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> }'},
475 'h(elp)?|\\?' => {
476 'output' => 'exit
477 meminfo
478 mem_usage
479 User> '
480 },
481 'mem_usage' => {'do' => 'my ( $tot,$avail) = (split /\\n/ ,do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> })[0,2];$tot =~ s/\\D*//g; $avail =~ s/\\D*//g; sprintf "%0.2f%%\\n",(100*($tot-$avail)/$tot); '},
482 },
483 'not_matching_info' => '% Unknown command or computer name, or unable to find computer address',
484 'output' => 'User> '
485 }
486 );
487  
488  
489  
490 The keys are the states name.
491 "expect" contain a sub HASH where the keys are word or REGEX expected as input
492  
493  
494 =over 1
495  
496 =item In this hash, you have:
497  
498 =back
499  
500 =over 2
501  
502 =item executable code:
503  
504 "do" for perl code
505  
506  
507 "exec" for system code,
508  
509  
510 and 4 dedicated commands:
511  
512 "cat" just reading the file content provided in parameter).
513  
514 "catRAND" chose randomly one of the files provided in parameter space separated
515  
516 "catWRAND" chose randomly (weighted) one of the files provided in parameter space separatedwith a : to separate the weight
517  
518 e.g. 'catWRAND' => './t/test_cat.txt:1 ./t/test_cat1.txt:50', in this case the file ./t/test_cat1.txt get 50 more chance to be selected than file ./t/test_cat.txt
519  
520 "catSEQ" read sequentialy the next files provided in parameter space separated
521 if "catSEQ_idx" is defined, that file is used to keep the state. Otherwise , the state file is named used
522 all the files name from "catSEQ" concatenated with a final '.tate'. All spaces are replaced by an underscore
523  
524 "catSEQn" read sequentialy a list of files but a specific number of time provided in parameter space separated
525 if "catSEQn_idx" is defined, that file is used to keep the state. Otherwise , the state file is named used
526 all the files name from "catSEQ" concatenated with a final '.tate'. All spaces are replaced by an underscore
527 e.g.
528 'cat' => {
529 'catSEQn' => '5,./t/test_cat.txt 2,./t/test_cat1.txt',
530 'catSEQn_idx' => './t/test_cat_seqn.idx',
531 'final' => 0
532 },
533 This setup read 5 times the first file followed by 2 times the second file
534  
535 =back
536  
537 =over 3
538  
539 =item * It is possible to use a regex to allow optional commands
540  
541  
542 e.g.
543 "h(elp)?|\\?": {
544 "output": "default\nexit\ntimeoutA\n__PROMPT__"
545 }
546  
547 This json section match for 'h' 'help' or '?'
548  
549  
550  
551 In the regex, the group capture could be used in the command parameter as a substitution
552  
553 1st group is substituted by __1__
554  
555 2nd group is substituted by __2__
556  
557 ...
558  
559 e.g. for a ping
560  
561 "ping (.*)" => {"exec" => "ping -c 3 __1__"},
562  
563 The __1__ is substitued by the first parameter in the expected command
564  
565 If you run the command "ping 127.0.0.1" the exec run the command "ping -c 3 127.0.0.1"
566  
567 Other example:
568  
569  
570 "cat /tmp/test/((\\w)(\\d))": {
571 "cat": "/tmp/test/__3__"
572 }
573  
574 If you call with "cat /tmp/test/a1 the cat read the file /tmp/test/1" (without the 'a' because the group 3 is matching a single digit after a single character
575  
576  
577  
578 =item * If you need to match all possible partial match for a word ( like "h, "he", "hel", "help" to match the "help" state)
579 Use the extra tag "swapregex": "1"
580  
581 In case of "swapregex": "1" we use this
582  
583 $key =~ /^$in/
584  
585 e.g.
586  
587 'expect' => {
588 'not_matching' => 'prompt',
589 'help' => {
590 'swapregex' => 1,
591 'output' => 'in enable',
592 'final' => 0
593 }
594 }
595  
596 In this example me match "h, "he", "hel", "help"
597  
598  
599 This reverse the REGEX test
600 In normal case (no "swapregex" )
601 The state is check with
602  
603 $in =~ /^$key$/
604  
605 Where $in is the input from the run() function
606 and $key is the state defined in the HASH
607  
608  
609  
610 =item * If you need to match all possible abbreviation starting from a fixed header you use the tag 'alternation'=>1 and the variable part between sqare bracket.
611  
612  
613 e.g.
614  
615 'ot[her]' => {
616 'alternation' => 1,
617 'output' => 'in enable',
618 'final' => 0
619 }
620  
621 This match 'oth', 'othe' and 'other'
622  
623 In fact the system create the REGEX /^o(t|th|the|ther)$/
624  
625 For a more complex example:
626  
627 'c[onfiguration] t[erminal]'
628  
629 create the REGEX
630  
631 /^c(o|on|onf|onfi|onfig|onfigu|onfigur|onfigura|onfigurat|onfigurati|onfiguratio|onfiguration) t(e|er|erm|ermi|ermin|ermina|erminal)$/
632  
633 =back
634  
635 =over 4
636  
637 =item * It is possible to do a case insensitive matching with the special tag "caseinsensitive": "1"
638  
639 =over 4
640  
641 e.g.
642  
643 'other' => {
644 'output' => 'in other',
645 'caseinsensitive' => 1,
646 'final' => 0
647 }
648  
649 in this example "other", "OTHER", "Other", "otheR" (and all other case alternate set) match the state "other"
650  
651 =back
652  
653 =over 4
654  
655 "matching"
656 to define the state when the input match the "expect" value
657  
658 =back
659  
660 =over 4
661  
662 =item "final"
663 to return a flag
664  
665 =back
666  
667 =over 4
668  
669 =item "not_matching"
670 the state if the input is not matching the "expect" value (if missing stay in the same state)
671  
672 =back
673  
674 =over 4
675  
676 =item "repeat"
677 a number of trial before the state goes to "not_matching0"
678  
679 =back
680  
681 =over 4
682  
683 =item "not_matching0"
684 the state when the number of failled matching number is reached
685  
686  
687 =back
688  
689 =over 4
690  
691 =item "not_matching_info_last"
692 info returned as second value when the failled matching number is reached
693  
694 =back
695  
696 =over 4
697  
698 =item "output"
699 the info returned as second value
700  
701 =back
702  
703 =over 4
704  
705 =item "output_file"
706 use the content of that file as output
707  
708  
709 =back
710  
711 It is perfectly possible to add extra tag not used by FSM::Basic for generic purpose.
712 These extra tag are returned from the run() function as third parameter HASH ref .
713 Check examples/fake_bash_ssh1.*
714 Take a look at timout and timer usage
715 In this example if destination IP from the SSH connection is available, the file IP.json is used as definition
716 (with fallback to fake_bash1.pl)
717  
718 =cut
719  
720 =back
721  
722 B<run>
723  
724 my ( $final, $out, $extra ) = $fsm->run( $in );
725  
726 Run the FSM with the input and return the final state (0 or 1) , the expected output and a reference to an HASH with the extra tag key/value pair
727  
728  
729 =cut
730  
731 =over 1
732  
733 =back
734  
735  
736 B<EXAMPLE>
737  
738  
739 use strict;
740 use feature qw( say );
741 use FSM::Basic;
742 use JSON;
743 use Term::ReadLine;
744  
745 my %states = (
746 'accept' => {
747 'expect' => {
748 'default' => {
749 'final' => 0,
750 'matching' => 'prompt'
751 }
752 },
753 'not_matching' => 'accept',
754 'not_matching0' => 'close',
755 'not_matching_info_last' => '% Bad passwords
756 ',
757 'output' => 'Password: ',
758 'repeat' => 2
759 },
760  
761 'close' => {'final' => 1},
762 'prompt' => {
763 'expect' => {
764 'not_matching' => 'prompt',
765 'exit' => {
766 'matching' => 'close',
767 'final' => 0
768 },
769 "read" => {'cat' => 'file.txt'},
770 "read_random" => {'catRAND' => 'file1.txt file2.txt file3.txt'},
771 "read_seq" => {'catSEQ' => 'file1.txt file2.txt file3.txt', 'catSEQ_idx' => 'catSEQ_status'},
772 'meminfo' => {'do' => 'do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> }'},
773 'mem' => {
774 'do' => "my ( $tot,$avail) = (split /\n/ ,do { local( @ARGV, $/ ) = \"/proc/meminfo\" ; <> })[0,2];$tot =~ s/\\D*//g; $avail =~ s/\\D*//g; sprintf \"%0.2f%%\\n\",(100*($tot-$avail)/$tot);"
775 },
776 'h(elp)?|\\?' => {
777 'output' => 'exit
778 read
779 read_random
780 read_seq
781 meminfo
782 mem_usage
783 mem
784 User> '
785 },
786 'mem_usage' => {'do' => 'my ( $tot,$avail) = (split /\\n/ ,do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> })[0,2];$tot =~ s/\\D*//g; $avail =~ s/\\D*//g; sprintf "%0.2f%%\\n",(100*($tot-$avail)/$tot); '},
787 },
788 'not_matching_info' => '% Unknown command or computer name, or unable to find computer address',
789 'output' => 'User> '
790 }
791 );
792 my $history_file = glob( '/tmp/fsm.history' );
793 my $prompt = '> ';
794 my $line;
795 my $final = 0;
796 my $extra;
797 my $term = new Term::ReadLine 'bash';
798 my $attribs = $term->Attribs->ornaments( 0 );
799 $term->using_history();
800 $term->read_history( $history_file );
801 $term->clear_signals();
802  
803 my $fsm = FSM::Basic->new( \%states, 'accept' );
804 my $out = "Password> ";
805 while ( defined( $line = $term->readline( $out ) ) )
806 {
807 ( $final, $out, $extra ) = $fsm->run( $line );
808 $term->write_history( $history_file );
809 last if $final;
810 }
811  
812 print $out if $final;
813  
814  
815 More sample code in the examples folder.
816  
817  
818 TODO
819  
820 add "edit" to allow on the fly modification of the states definition
821  
822 add "verify_states" to check all states are reachable from a original state
823  
824 B<SEE ALSO>
825  
826 FSA::Rules
827  
828 https://metacpan.org/pod/FSA::Rules
829  
830  
831 B<AUTHOR>
832  
833 DULAUNOY Fabrice, C<< <fabrice at dulaunoy.com> >>
834  
835 B<BUGS>
836  
837 Please report any bugs or feature requests to C<bug-FSM-basic at rt.cpan.org>, or through
838 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FSM-Basic>. I will be notified, and then you'll
839 automatically be notified of progress on your bug as I make changes.
840  
841 B<SUPPORT>
842  
843 You can find documentation for this module with the perldoc command.
844  
845 perldoc FSM::Basic
846  
847  
848 You can also look for information at:
849  
850 =over 4
851  
852 =item * RT: CPAN's request tracker (report bugs here)
853  
854 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FSM-Basic>
855  
856 =item * AnnoCPAN: Annotated CPAN documentation
857  
858 L<http://annocpan.org/dist/FSM-Basic>
859  
860 =item * CPAN Ratings
861  
862 L<http://cpanratings.perl.org/d/FSM-Basic>
863  
864 =item * Search CPAN
865  
866 L<http://search.cpan.org/dist/FSM-Basic/>
867  
868 =back
869  
870  
871 B<ACKNOWLEDGEMENTS>
872  
873  
874 B<LICENSE AND COPYRIGHT>
875  
876 Copyright 2008 - 2020 DULAUNOY Fabrice.
877  
878 This program is free software; you can redistribute it and/or modify it
879 under the terms of the the Artistic License (2.0). You may obtain a
880 copy of the full license at:
881  
882 L<http://www.perlfoundation.org/artistic_license_2_0>
883  
884 Any use, modification, and distribution of the Standard or Modified
885 Versions is governed by this Artistic License. By using, modifying or
886 distributing the Package, you accept this license. Do not use, modify,
887 or distribute the Package, if you do not accept this license.
888  
889 If your Modified Version has been derived from a Modified Version made
890 by someone other than you, you are nevertheless required to ensure that
891 your Modified Version complies with the requirements of this license.
892  
893 This license does not grant you the right to use any trademark, service
894 mark, tradename, or logo of the Copyright Holder.
895  
896 This license includes the non-exclusive, worldwide, free-of-charge
897 patent license to make, have made, use, offer to sell, sell, import and
898 otherwise transfer the Package with respect to any patent claims
899 licensable by the Copyright Holder that are necessarily infringed by the
900 Package. If you institute patent litigation (including a cross-claim or
901 counterclaim) against any party alleging that the Package constitutes
902 direct or contributory patent infringement, then this Artistic License
903 to you shall terminate on the date that such litigation is filed.
904  
905 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
906 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
907 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
908 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
909 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
910 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
911 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
912 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
913  
914  
915 =cut
916  
917 7 1; # End of FSM::Basic
↑ Summary