blib/lib/Acme/Chef/Recipe.pm

filter: Statementstmt 90.2% 362 / 401 Branchbran 82.8% 111 / 134 Conditioncond 72.5% 45 / 62 MC/DCmcdc 43.7% 14 / 32 Subroutinesub 85.4% 47 / 55 Podpod 100% 9 / 9 totaltotal 84.8% 588 / 693 CC 105 SCAR 18.4
CC
105
Cov
87%
compile
19.7
execute
13.2
require_ingredient
8.1
CRAP 6.3
« blib/lib/Acme/Chef.pm ↑ Summary Build »
1  
2 package Acme::Chef::Recipe;
3  
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 Acme::Chef::Ingredient;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Acme::Chef::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Acme::Chef::Recipe - Internal module used by Acme::Chef
15  
16 =head1 SYNOPSIS
17  
18 use Acme::Chef;
19  
20 =head1 DESCRIPTION
21  
22 Please see L<Acme::Chef>;
23  
24 =head2 METHODS
25  
26 This is list of methods in this package.
27  
28 =over 2
29  
30 =cut
31  
32 7 ▶use vars qw/$VERSION %Grammars @GrammarOrder %Commands/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 $VERSION = '1.00';
34  
35 7 @GrammarOrder = qw(
36 take_from add_dry put fold add remove combine divide
37 liquify_contents liquify stir_time stir_ingredient
38 mix clean pour refrigerate set_aside serve_with
39 until_verbed verb
40 );
41  
42 { # scope of grammar definition
43  
44 7 my $ord = qr/([1-9]\d*)(?:st|nd|rd|th)/;
45 7 my $ord_noncap = qr/[1-9]\d*(?:st|nd|rd|th)/;
46 7 my $ingr_noncap = qr/[\-\w][\- \w]*/;
47 7 my $ingr = qr/($ingr_noncap)/;
48 7 my $verb = qr/([\-\w]+)/;
49  
50 %Grammars = (
51  
52 put => sub {
53 178 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
54 178 local $_ = shift;
55 178 my $regex;
56 178 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 75 103 178
57 75 $regex = qr/^Put (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
58 } else {
59 103 $regex = qr/^Put (?:the )?$ingr$/;
60 }
61 178 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 117 61 178
62 61 ▶ $recipe->require_bowl($2||1);
Condition100%2/2
$2 || 1
l!l
1150
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
63 61 $recipe->require_ingredient($1, 'put');
64 61 ▶ return 'put', $1, ($2||1);
Condition100%2/2
$2 || 1
l!l
1150
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
65 },
66  
67 take_from => sub {
68 183 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
69 183 local $_ = shift;
70 183 ▶ /^Take $ingr from refrigerator$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Take $ingr from refrigerator$/ or return () 182 1 183
71 1 $recipe->require_ingredient($1);
72 1 return 'take_from', $1;
73 },
74  
75 fold => sub {
76 117 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=14.0
77 117 local $_ = shift;
78 117 ▶ /^Fold (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Fold (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/ or return () 113 4 117
79 4 ▶ $recipe->require_bowl($2||1);
Condition50%1/2
$2 || 1
l!l
04
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
80 4 $recipe->require_ingredient($1, 'fold');
81 4 ▶ return 'fold', $1, ($2||1);
Condition50%1/2
$2 || 1
l!l
04
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
82 },
83  
84 add => sub {
85 113 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=5 SCAR=16.2
86 113 local $_ = shift;
87 113 my $regex;
88 113 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 10 103 113
89 10 $regex = qr/^Add (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
90 } else {
91 103 $regex = qr/^Add (?:the )?$ingr()$/;
92 }
93 113 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 111 2 113
94 2 ▶ $recipe->require_bowl($2||1);
Condition50%1/2
$2 || 1
l!l
20
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
95 2 $recipe->require_ingredient($1, 'add');
96 2 ▶ return 'add', $1, ($2||1);
Condition50%1/2
$2 || 1
l!l
20
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
97 },
98  
99 remove => sub {
100 111 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
101 111 local $_ = shift;
102 111 my $regex;
103 111 ▶ if (/ from (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ from (?:the )?(?:$ord )?mixing bowl$/) { } 55 56 111
104 55 $regex = qr/^Remove (?:the )?$ingr from (?:the )?(?:$ord )?mixing bowl$/;
105 } else {
106 56 $regex = qr/^Remove (?:the )?$ingr()$/;
107 }
108 111 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 55 56 111
109 56 ▶ $recipe->require_bowl($2||1);
Condition100%2/2
$2 || 1
l!l
551
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
110 56 $recipe->require_ingredient($1, 'remove');
111 56 ▶ return 'remove', $1, ($2||1);
Condition100%2/2
$2 || 1
l!l
551
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
112 },
113  
114 combine => sub {
115 55 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
116 55 local $_ = shift;
117 55 my $regex;
118 55 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 8 47 55
119 8 $regex = qr/^Combine (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
120 } else {
121 47 $regex = qr/^Combine (?:the )?$ingr()$/;
122 }
123 55 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 51 4 55
124 4 ▶ $recipe->require_bowl($2||1);
Condition100%2/2
$2 || 1
l!l
22
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
125 4 $recipe->require_ingredient($1, 'combine');
126 4 ▶ return 'combine', $1, ($2||1);
Condition100%2/2
$2 || 1
l!l
22
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
127 },
128  
129 divide => sub {
130 51 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=5 SCAR=16.2
131 51 local $_ = shift;
132 51 my $regex;
133 51 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 6 45 51
134 6 $regex = qr/^Divide (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
135 } else {
136 45 $regex = qr/^Divide(?: the)?$ingr()$/;
137 }
138 51 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 47 4 51
139 4 ▶ $recipe->require_bowl($2||1);
Condition50%1/2
$2 || 1
l!l
40
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
140 4 $recipe->require_ingredient($1, 'divide');
141 4 ▶ return 'divide', $1, ($2||1);
Condition50%1/2
$2 || 1
l!l
40
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
142 },
143  
144 add_dry => sub {
145 182 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=14.1
146 182 local $_ = shift;
147 182 ▶ /^Add (?:the )?dry ingredients(?: into (?:the )?(?:$ord )?mixing bowl)?$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Add (?:the )?dry ingredients(?: into (?:the )?(?:$ord )?mixing bowl)?$/ or return () 178 4 182
148 4 ▶ $recipe->require_bowl($1||1);
Condition50%1/2
$1 || 1
l!l
40
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
149 4 ▶ return 'add_dry', ($1||1);
Condition50%1/2
$1 || 1
l!l
40
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
150 },
151  
152 liquify_contents => sub {
153 47 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
154 47 local $_ = shift;
155 47 ▶ /^Liquify (?:the )?contents of (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Liquify (?:the )?contents of (?:the )?(?:$ord )?mixing bowl$/ or return () 41 6 47
156 6 ▶ $recipe->require_bowl($1||1);
Condition100%2/2
$1 || 1
l!l
42
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
157 6 ▶ return 'liquify_contents', ($1||1);
Condition100%2/2
$1 || 1
l!l
42
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
158 },
159  
160 liquify => sub {
161 41 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
162 41 local $_ = shift;
163 41 ▶ /^Liquify (?:the )?$ingr$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Liquify (?:the )?$ingr$/ or return () 40 1 41
164 1 $recipe->require_ingredient($1, 'liquify');
165 1 return 'liquify', $1;
166 },
167  
168 stir_time => sub {
169 40 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
170 40 local $_ = shift;
171 40 ▶ /^Stir (?:(?:the )?(?:$ord )?mixing bowl )?for (\d+) minutes?$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Stir (?:(?:the )?(?:$ord )?mixing bowl )?for (\d+) minutes?$/ or return () 37 3 40
172 3 ▶ $recipe->require_bowl($1||1);
Condition100%2/2
$1 || 1
l!l
21
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
173 3 ▶ return 'stir_time', $2, ($1||1);
Condition100%2/2
$1 || 1
l!l
21
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
174 },
175  
176 stir_ingredient => sub {
177 37 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=14.0
178 37 local $_ = shift;
179 37 ▶ /^Stir $ingr into (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Stir $ingr into (?:the )?(?:$ord )?mixing bowl$/ or return () 35 2 37
180 2 ▶ $recipe->require_bowl($2||1);
Condition50%1/2
$2 || 1
l!l
20
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
181 2 $recipe->require_ingredient($1, 'stir_ingredient');
182 2 ▶ return 'stir_ingredient', $1, ($2||1);
Condition50%1/2
$2 || 1
l!l
20
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
183 },
184  
185 mix => sub {
186 35 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=14.1
187 35 local $_ = shift;
188 35 ▶ /^Mix (?:the (?:$ord )?mixing bowl )well$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Mix (?:the (?:$ord )?mixing bowl )well$/ or return () 34 1 35
189 1 ▶ $recipe->require_bowl($1||1);
Condition50%1/2
$1 || 1
l!l
10
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
190 1 ▶ return 'mix', ($1||1);
Condition50%1/2
$1 || 1
l!l
10
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
191 },
192  
193 clean => sub {
194 34 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=14.1
195 34 local $_ = shift;
196 34 ▶ /^Clean (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Clean (?:the )?(?:$ord )?mixing bowl$/ or return () 31 3 34
197 3 ▶ $recipe->require_bowl($1||1);
Condition50%1/2
$1 || 1
l!l
03
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
198 3 ▶ return 'clean', ($1||1);
Condition50%1/2
$1 || 1
l!l
03
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
199 },
200  
201 pour => sub {
202 31 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
203 31 local $_ = shift;
204 31 ▶ /^Pour contents of (?:the )?((?:[1-9]\d*(?:st|nd|rd|th) )?)mixing bowl into (?:the )?((?:[1-9]\d*(?:st|nd|rd|th) )?)baking dish$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Pour contents of (?:the )?((?:[1-9]\d*(?:st|nd|rd|th) )?)mixing bowl into (?:the )?((?:[1-9]\d*(?:st|nd|rd|th) )?)baking dish$/ or return () 15 16 31
205 16 ▶ my $m = $1 || 1;
Condition100%2/2
$1 || 1
l!l
106
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
206 16 ▶ my $b = $2 || 1;
Condition100%2/2
$2 || 1
l!l
106
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
207 16 $m =~ s/\D//g;
208 16 $b =~ s/\D//g;
209 16 $recipe->require_bowl($m);
210 16 $recipe->require_dish($b);
211 16 return 'pour', $m, $b;
212 },
213  
214 refrigerate => sub {
215 15 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
216 15 local $_ = shift;
217 15 ▶ /^Refrigerate(?: for (\d+) hours?)?$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Refrigerate(?: for (\d+) hours?)?$/ or return () 11 4 15
218 4 ▶ return 'refrigerate', (defined $1 ? $1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 3 1 4
219 },
220  
221 set_aside => sub {
222 11 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=7.6
223 11 local $_ = shift;
224 11 ▶ /^Set aside$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Set aside$/ or return () 11 0 11
225 0 return 'set_aside';
226 },
227  
228 serve_with => sub {
229 11 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
230 11 local $_ = shift;
231 11 ▶ /^Serve with $ingr$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Serve with $ingr$/ or return () 10 1 11
232 # $ingr is a recipe name here
233 1 return 'serve_with', lc($1);
234 },
235  
236 verb => sub {
237 5 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
238 5 local $_ = shift;
239 5 ▶ /^$verb (?:the )?$ingr$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^$verb (?:the )?$ingr$/ or return () 0 5 5
240 5 $recipe->require_ingredient($2, 'verb');
241 5 return 'verb', lc($1), $2;
242 },
243  
244 until_verbed => sub {
245 10 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
246 10 local $_ = shift;
247 10 ▶ /^$verb ((?:(?:the )?$ingr_noncap )?)until ${verb}ed$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^$verb ((?:(?:the )?$ingr_noncap )?)until ${verb}ed$/ or return () 5 5 10
248 5 ▶ my $ing = (defined $2 ? $2 : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 5 0 5
249 5 my $verbed = $3;
250 5 ▶ $verbed .= 'e' if not exists $recipe->{loops}{$verbed};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $recipe->{"loops"}{$verbed} 3 2 5
251 5 $ing =~ s/^the //;
252 5 $ing =~ s/ $//;
253 5 ▶ $recipe->require_ingredient($ing, 'until_verbed') if $ing ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $ing ne "" 4 1 5
254 5 return 'until_verbed', $verbed, $ing;
255 },
256  
257 7 );
258  
259 }
260  
261 %Commands = (
262 put => sub {
263 608 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
264 608 my $data = shift;
265 $recipe -> {bowls} -> [$data -> [2] - 1]
266 ->
267 608 put( $recipe -> {ingredients} -> {$data -> [1]} );
268 608 return 1;
269 },
270
271 take_from => sub {
272 2 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
273 2 my $data = shift;
274 2 local $/ = "\n";
275 2 my $value;
276 2 while (1) {
277 2 $value = <STDIN>;
278 2 ▶ last if $value =~ /^\s*\.?\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\s*\.?\d+/ 2 0 2
279 }
280 2 $recipe -> {ingredients} -> {$data -> [1]}
281 -> value($value+0);
282 },
283  
284 fold => sub {
285 111 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
286 111 my $data = shift;
287 $recipe -> {bowls} -> [$data -> [2] - 1]
288 ->
289 111 fold( $recipe -> {ingredients} -> {$data -> [1]} );
290 111 return 1;
291 },
292  
293 add => sub {
294 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 my $data = shift;
296 $recipe -> {bowls} -> [$data -> [2] - 1]
297 ->
298 0 add( $recipe -> {ingredients} -> {$data -> [1]} );
299 0 return 1;
300 },
301  
302 remove => sub {
303 168 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
304 168 my $data = shift;
305 $recipe -> {bowls} -> [$data -> [2] - 1]
306 ->
307 168 remove( $recipe -> {ingredients} -> {$data -> [1]} );
308 168 return 1;
309 },
310  
311 combine => sub {
312 240 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
313 240 my $data = shift;
314 $recipe -> {bowls} -> [$data -> [2] - 1]
315 ->
316 240 combine( $recipe -> {ingredients} -> {$data -> [1]} );
317 240 return 1;
318 },
319  
320 divide => sub {
321 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 0 my $data = shift;
323 $recipe -> {bowls} -> [$data -> [2] - 1]
324 ->
325 0 divide( $recipe -> {ingredients} -> {$data -> [1]} );
326 0 return 1;
327 },
328  
329 add_dry => sub {
330 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
331 0 my $data = shift;
332 $recipe -> {bowls} -> [$data -> [1] - 1]
333 ->
334 put_sum(
335 0 grep { $_->type() eq 'dry' }
336 0 values %{ $recipe -> {ingredients} }
337 );
338 0 return 1;
339 },
340  
341 liquify => sub {
342 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
343 0 my $data = shift;
344 0 $recipe -> {ingredients} -> {$data -> [1]} -> liquify();
345 0 return 1;
346 },
347  
348 liquify_contents => sub {
349 6 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
350 6 my $data = shift;
351 6 $recipe -> {bowls} -> [$data -> [1] - 1] -> liquify_contents();
352 6 return 1;
353 },
354  
355 stir_time => sub {
356 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 0 my $data = shift;
358 0 $recipe -> {bowls} -> [$data -> [2] - 1]
359 ->
360 stir_time( $data -> [1] );
361 0 return 1;
362 },
363  
364 stir_ingredient => sub {
365 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
366 0 my $data = shift;
367 $recipe -> {bowls} -> [$data -> [2] - 1]
368 ->
369 0 stir_ingredient( $recipe -> {ingredients} -> {$data -> [1]} );
370 0 return 1;
371 },
372  
373 mix => sub {
374 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 0 my $data = shift;
376 0 $recipe -> {bowls} -> [$data -> [1] - 1] -> mix();
377 0 return 1;
378 },
379  
380 clean => sub {
381 72 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
382 72 my $data = shift;
383 72 $recipe -> {bowls} -> [$data -> [1] - 1] -> clean();
384 72 return 1;
385 },
386  
387 pour => sub {
388 77 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
389 77 my $data = shift;
390 77 my @stuff = $recipe -> {bowls} -> [$data -> [1] - 1] -> pour();
391  
392 77 $recipe -> {dishes} -> [$data -> [2] - 1] -> put( $_ ) foreach @stuff;
393  
394 77 return 1;
395 },
396  
397 refrigerate => sub {
398 8 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
399 8 my $data = shift;
400 8 my $serves = $recipe->{serves};
401 8 my $hours = $data->[1];
402 8 ▶ $serves ||= 0;
Condition100%2/2
$serves ||= 0
l!l
35
MC/DC100%1/1
$serves ||= 0
$serves
Truth table100%2/2
A
A= $serves
Aresult
11
00
403 8 ▶ $hours ||= 0;
Condition100%2/2
$hours ||= 0
l!l
53
MC/DC100%1/1
$hours ||= 0
$hours
Truth table100%2/2
A
A= $hours
Aresult
11
00
404 8 ▶ $recipe->{serves} = $hours if $serves < $hours;
Branch: 1 branch
BranchTrueFalseTotal
if $serves < $hours 5 3 8
405 8 return 'halt';
406 },
407  
408 set_aside => sub {
409 0 ▶ my $recipe = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
410 0 my $data = shift;
411  
412 0 return 'break';
413 },
414  
415 serve_with => sub {
416 36 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
417 36 my $data = shift;
418  
419 36 my $rec_recipe = $data->[1];
420  
421 36 return "recurse.$rec_recipe" ;
422 },
423  
424 verb => sub {
425 624 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
426 624 my $data = shift;
427  
428 624 my $verb = $data->[1];
429 624 my $ingr = $data->[2];
430 624 return "loop.$verb.$ingr";
431 },
432  
433 until_verbed => sub {
434 576 ▶ my $recipe = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
435 576 my $data = shift;
436  
437 576 my $verb = $data->[1];
438  
439 576 ▶ if ( exists $recipe->{ingredients}->{$data->[2]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $recipe->{"ingredients"}{$data->[2]}) 576 0 576
440 576 my $ingr = $recipe->{ingredients}->{$data->[2]};
441 576 $ingr->value( $ingr->value() - 1 );
442 }
443  
444 576 return "endloop.$verb";
445 },
446  
447 7 );
448  
449 =item new
450  
451 Acme::Chef::Recipe constructor. Arguments are interpreted as key/value pairs
452 and used as object attributes.
453  
454 =cut
455  
456  
457 sub new {
458 96 ▶ my $proto = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
459 96 ▶ my $class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
8970
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
460  
461 96 my $self = {};
462  
463 96 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 89 7 96
464 89 %$self = %$proto;
465  
466 89 $self->{bowls} = [ map { $_->new() } @{$self -> {bowls }} ];
467 89 $self->{dishes} = [ map { $_->new() } @{$self -> {dishes}} ];
468 84 $self->{loops} = { map { ( $_, $self->{loops}{$_} ) }
469 89 keys %{$self->{loops}} };
470  
471 89 ▶ if ( $self->{compiled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"compiled"}) 89 0 89
472 $self->{ingredients} = { map {
473 (
474 $_,
475 305 $self -> {ingredients} -> {$_} -> new()
476 )
477 89 } keys %{ $self->{ingredients} }
478 };
479 }
480 }
481  
482 96 my %args = @_;
483  
484 96 %$self = (
485 compiled => 0,
486 name => '',
487 comments => '',
488 ingredients => '',
489 cooking_time => '',
490 temperature => '',
491 method => '',
492 serves => '',
493 output => '',
494 loops => {},
495 bowls => [],
496 dishes => [],
497 %$self,
498 %args,
499 );
500  
501 96 bless $self => $class;
502 96 return $self;
503 }
504  
505  
506 =item execute
507  
508 Executes the recipe (program). First argument should be a reference to a
509 hash of sous-recipes.
510  
511 =cut
512  
513  
514 sub execute {
515 53 ▶ my $self = shift;
Subroutine execute: called CC=13 SCAR=25.8
Pod documented
516  
517 53 my $recipes = shift;
518  
519 53 ▶ $self->compile() unless $self->{compiled};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"compiled"} 0 53 53
520  
521 53 my @loop_stack;
522  
523 53 my $max_pos = $#{$self->{method}};
524 53 my $exec_pos = 0;
525 53 while (1) {
526  
527 2528 my $next_method = $self->{method}->[$exec_pos];
528 # print ' ' x scalar(@loop_stack), join(',', @$next_method),"\n";
529  
530 2528 my $return = $Commands{$next_method->[0]}->($self, $next_method);
531  
532 2528 ▶ last if $return eq 'halt';
Branch: 1 branch
BranchTrueFalseTotal
if $return eq "halt" 8 2520 2528
533  
534 2520 ▶ if ( $return =~ /^recurse\.([\-\w][\-\w ]*)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($return =~ /^recurse\.([\-\w][\-\w ]*)/) { } 36 2484 2520
elsif ($return =~ /^loop\.([^\.]+)\.([^\.]+)/) { } 624 1860 2484
elsif ($return =~ /^endloop\.([^\.]+)/) { } 576 1284 1860
elsif ($return =~ /^break/) { } 0 1284 1284
535 36 ▶ exists $recipes->{$1}
Branch: 1 branch
BranchTrueFalseTotal
exists $recipes->{$1} or croak("Invalid recipe '$1' specified for recursion.") 0 36 36
536 or croak "Invalid recipe '$1' specified for recursion.";
537  
538 36 my $clone = $self->new();
539 my $sous_recipe = $recipes->{$1}->new(
540 bowls => $clone->{bowls},
541 dishes => $clone->{dishes},
542 36 );
543  
544 36 my $sous_done = $sous_recipe->execute( $recipes );
545  
546 36 $self->output( $sous_done->output() );
547  
548 36 $self -> {bowls} -> [0]
549 -> put( $sous_done -> first_bowl() -> new() -> pour() );
550  
551 } elsif ( $return =~ /^loop\.([^\.]+)\.([^\.]+)/ ) {
552 624 my $verb = $1;
553 624 my $ingr = $2;
554  
555 624 push @loop_stack, $verb;
556  
557 624 ▶ if ( not $self -> {ingredients} -> {$ingr} -> value() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ingredients"}{$ingr}->value) 45 579 624
558 45 pop @loop_stack;
559 45 $exec_pos = $self -> {loops} -> {$verb} -> {end};
560 }
561  
562 } elsif ( $return =~ /^endloop\.([^\.]+)/ ) {
563 576 my $verb = $1;
564  
565 576 $exec_pos = $self -> {loops} -> {$verb} -> {start} - 1;
566  
567 } elsif ( $return =~ /^break/ ) {
568 0 my $verb = pop @loop_stack;
569 0 $exec_pos = $self -> {loops} -> {$verb} -> {end};
570 }
571  
572 2520 $exec_pos++;
573 2520 ▶ last if $exec_pos > $max_pos;
Branch: 1 branch
BranchTrueFalseTotal
if $exec_pos > $max_pos 45 2475 2520
574 }
575  
576 53 ▶ if ( $self->{serves} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"serves"}) 17 36 53
577 17 foreach my $serve ( 0..($self->{serves}-1) ) {
578 26 ▶ last if $serve > $#{$self->{dishes}};
Branch: 1 branch
BranchTrueFalseTotal
if $serve > $#{$$self{"dishes"};} 0 26 26
579 26 my $string = $self->{dishes}->[$serve]->print();
580 26 $self->{output} .= $string;
581 }
582 }
583  
584 53 return $self;
585 }
586  
587 =item first_bowl
588  
589 Returns the first bowl of the recipe.
590  
591 =cut
592  
593 sub first_bowl {
594 36 ▶ my $self = shift;
Subroutine first_bowl: called CC=1 SCAR=0.0
Pod documented
595 36 return $self->{bowls}->[0];
596 }
597  
598 =item require_ingredient
599  
600 First argument must be an ingredient object. Second may be a string indicating
601 the location of the requirement. Throws a fatal error if the ingredient is not
602 present.
603  
604 =cut
605  
606 sub require_ingredient {
607 144 ▶ my $self = shift;
Subroutine require_ingredient: called CC=5 SCAR=20.9
Pod documented
608 144 my $ingredient = shift;
609 144 my $sub = shift;
610  
611 144 ▶ (defined $ingredient and exists $self->{ingredients}{$ingredient})
Branch: 3 branches
BranchTrueFalseTotal
defined $ingredient and exists $self->{"ingredients"}{$ingredient} or croak(q[Unknown ingredient '] . (defined $ingredient ? $ingredient : "<undefined>") . qq[' required for recipe '$self->{"name"}'] . (defined $sub ? " in '${sub}'" : "") . '.') 0 144 144
defined $ingredient ? : 0 0 0
defined $sub ? : 0 0 0
Condition33%1/3
defined $ingredient and exists $self->{"ingredients"}{$ingredient}
!ll&&!rl&&r
00144
MC/DC0%0/2
defined $ingredient and exists $self->{"ingredients"}{$ingredient}
defined $ingredient exists $self->{"ingredients"}{$ingredient}
Truth table33%1/3
A and B
A= defined $ingredientB= exists $self->{"ingredients"}{$ingredient}
ABresult
0X0
100
111
612 or croak "Unknown ingredient '".(defined$ingredient?$ingredient:'<undefined>').
613 "' required for recipe '$self->{name}'".
614 (defined $sub?" in '$sub'":'').".";
615  
616 144 return $self;
617 }
618  
619 =item output
620  
621 Mutator for the Recipe output.
622  
623 =cut
624  
625 sub output {
626 89 ▶ my $self = shift;
Subroutine output: called CC=2 SCAR=6.9
Pod documented
627  
628 89 ▶ $self->{output} .= shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 36 53 89
629  
630 89 return $self->{output};
631 }
632  
633 =item require_bowl
634  
635 First argument must be a number of bowls. Additional bowls are added to the
636 recipe if it currently has less than this number of bowls.
637  
638 =cut
639  
640 sub require_bowl {
641 166 ▶ my $self = shift;
Subroutine require_bowl: called CC=3 SCAR=11.0
Pod documented
642 166 my $no = shift;
643  
644 166 ▶ return if @{$self->{bowls}} >= $no;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"bowls"};} >= $no 149 17 166
645  
646 17 ▶ while (@{$self->{bowls}} < $no) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"bowls"};} < $no) 17 17 34
647 17 push @{$self->{bowls}}, Acme::Chef::Container->new();
648 }
649  
650 17 return $self;
651 }
652  
653  
654 =item require_dish
655  
656 First argument must be a number of dishes. Additional dishes are added to the
657 recipe if it currently has less than this number of dishes.
658  
659 =cut
660  
661 sub require_dish {
662 16 ▶ my $self = shift;
Subroutine require_dish: called CC=3 SCAR=11.0
Pod documented
663 16 my $no = shift;
664  
665 16 ▶ return if @{$self->{dishes}} >= $no;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"dishes"};} >= $no 7 9 16
666  
667 9 ▶ while (@{$self->{dishes}} < $no) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"dishes"};} < $no) 9 9 18
668 9 push @{$self->{dishes}}, Acme::Chef::Container->new();
669 }
670  
671 9 return $self;
672 }
673  
674 =item recipe_name
675  
676 Mutator for the recipe name.
677  
678 =cut
679  
680 sub recipe_name {
681 13 ▶ my $self = shift;
Subroutine recipe_name: called CC=2 SCAR=7.1
Pod documented
682  
683 13 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 13 13
684  
685 13 return $self->{name};
686 }
687  
688  
689 =item compile
690  
691 Tries to compile the recipe. Returns 0 on error or if the recipe was
692 already compiled. Returns the compiled recipe if the compilation succeeded.
693  
694 =cut
695  
696 sub compile {
697 7 ▶ my $self = shift;
Subroutine compile: called CC=19 SCAR=29.8
Pod documented
698  
699 7 ▶ return 0 if $self->{compiled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"compiled"} 0 7 7
700  
701 7 my @ingredients = split /\n/, $self->{ingredients};
702  
703 7 shift @ingredients; # remove header line
704  
705 7 ▶ @ingredients or croak "Failed compiling recipe. No ingredients specified.";
Branch: 1 branch
BranchTrueFalseTotal
@ingredients or croak("Failed compiling recipe. No ingredients specified.") 0 7 7
706  
707 7 my %ingredients;
708 7 my $ingredient_no = 0;
709  
710 7 foreach (@ingredients) {
711 44 $ingredient_no++;
712  
713 44 my $value;
714 44 ▶ if (s/^[ ]*(\d+)[ ]//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^[ ]*(\d+)[ ]//) { } 43 1 44
715 43 $value = $1;
716 } else {
717 1 $value = undef;
718 }
719  
720 44 my $measure_type = '';
721 44 foreach my $type ( keys %Acme::Chef::Ingredient::MeasureTypes ) {
722 88 ▶ if ( s/^\Q$type\E[ ]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\Q$type\E[ ]//) 0 88 88
723 0 $measure_type = $type;
724 0 last;
725 }
726 }
727  
728 44 my $measure = '';
729 44 foreach my $meas ( keys %Acme::Chef::Ingredient::Measures ) {
730 546 ▶ next if $meas eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $meas eq "" 35 511 546
731  
732 511 ▶ if ( s/^\Q$meas\E[ ]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\Q$meas\E[ ]//) 16 495 511
733 16 $measure = $meas;
734 16 last;
735 }
736 }
737  
738 /[ ]*([\-\w][\- \w]*)[ ]*$/
739 44 ▶ or croak "Invalid ingredient specification (ingredient no. $ingredient_no, name).";
Branch: 1 branch
BranchTrueFalseTotal
/[ ]*([\-\w][\- \w]*)[ ]*$/ or croak("Invalid ingredient specification (ingredient no. $ingredient_no, name).") 0 44 44
740  
741 44 my $ingredient_name = $1;
742  
743 44 my $ingredient = Acme::Chef::Ingredient->new(
744 name => $ingredient_name,
745 value => $value,
746 measure => $measure,
747 measure_type => $measure_type,
748 );
749  
750 44 $ingredients{$ingredient_name} = $ingredient;
751 }
752  
753 7 $self->{ingredients} = \%ingredients;
754  
755 7 $self->{method} =~ s/\s+/ /g;
756  
757 7 my @steps = split /\s*\.\s*/, $self->{method};
758  
759 7 shift @steps; # remove "Method."
760  
761 7 my $step_no = 0;
762 7 foreach my $step (@steps) {
763 183 $step_no++;
764  
765 183 foreach my $grammar (@GrammarOrder) {
766 1307 my @res = $Grammars{$grammar}->($self, $step);
767 1307 ▶ @res or next;
Branch: 1 branch
BranchTrueFalseTotal
@res or next 1124 183 1307
768  
769 183 ▶ if ( $res[0] eq 'verb' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res[0] eq 'verb') { } 5 178 183
elsif ($res[0] eq 'until_verbed') { } 5 173 178
770 5 my $verb = $res[1];
771 5 my $ingr = $res[2];
772  
773 5 $self->{loops}->{$verb} = {start => ($step_no-1), test => $ingr};
774 } elsif ( $res[0] eq 'until_verbed' ) {
775 5 my $verb = $res[1];
776 5 ▶ exists $self->{loops}->{$verb}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"loops"}{$verb} or croak("Loop end without loop start '${verb}'.") 0 5 5
777 or croak "Loop end without loop start '$verb'.";
778  
779 5 $self->{loops}->{$verb}->{end} = $step_no - 1;
780 }
781  
782 183 $step = [@res];
783 183 last;
784 }
785  
786 183 ▶ croak "Invalid method step (step no. $step_no): '$step'."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $step eq "ARRAY" 0 183 183
787 if not ref $step eq 'ARRAY';
788 }
789  
790 7 ▶ if ( grep { not exists $self->{loops}{$_}{end} } keys %{$self->{loops}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not exists $self->{"loops"}{$_}{"end"};} keys %{$$self{"loops"};}) 0 7 7
791 0 croak "Not all loop starting points have matching ends.";
792 }
793  
794 7 $self->{method} = \@steps;
795  
796 7 $self->{compiled} = 1;
797  
798 7 return $self;
799 }
800  
801  
802 __END__
« blib/lib/Acme/Chef.pm ↑ Summary Build »