Chef/Recipe.pm

filter: Statementstmt 91.2% 366 / 401 Branchbran 82.8% 111 / 134 Conditioncond 72.5% 45 / 62 MC/DCmcdc 43.7% 14 / 32 Subroutinesub 87.2% 48 / 55 Podpod 100% 9 / 9 totaltotal 85.5% 593 / 693 CC 105 SCAR 18.3
CC
105
Cov
87%
compile
19.7
execute
13.2
require_ingredient
8.1
CRAP 6.3
« Chef.pm ↑ Summary Chef/Ingredient.pm »
1  
2 package Acme::Chef::Recipe;
3  
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use Acme::Chef::Ingredient;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶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 10 ▶use vars qw/$VERSION %Grammars @GrammarOrder %Commands/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 10 $VERSION = '1.01';
34  
35 10 @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 10 my $ord = qr/([1-9]\d*)(?:st|nd|rd|th)/;
45 10 my $ord_noncap = qr/[1-9]\d*(?:st|nd|rd|th)/;
46 10 my $ingr_noncap = qr/[\-\w][\- \w]*/;
47 10 my $ingr = qr/($ingr_noncap)/;
48 10 my $verb = qr/([\-\w]+)/;
49  
50 %Grammars = (
51  
52 207 ▶ put => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
53 207 my $recipe = shift;
54 207 local $_ = shift;
55 207 my $regex;
56 207 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 90 117 207
57 90 $regex = qr/^Put (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
58 } else {
59 117 $regex = qr/^Put (?:the )?$ingr$/;
60 }
61 207 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 78 129 207
62 78 ▶ $recipe->require_bowl($2||1);
Condition100%2/2
$2 || 1
l!l
1662
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
63 78 $recipe->require_ingredient($1, 'put');
64 78 ▶ return 'put', $1, ($2||1);
Condition100%2/2
$2 || 1
l!l
1662
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
65 },
66  
67 212 ▶ take_from => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 212 my $recipe = shift;
69 212 local $_ = shift;
70 212 ▶ /^Take $ingr from refrigerator$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Take $ingr from refrigerator$/ or return () 1 211 212
71 1 $recipe->require_ingredient($1);
72 1 return 'take_from', $1;
73 },
74  
75 129 ▶ fold => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
76 129 my $recipe = shift;
77 129 local $_ = shift;
78 129 ▶ /^Fold (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Fold (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/ or return () 4 125 129
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 125 ▶ add => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
85 125 my $recipe = shift;
86 125 local $_ = shift;
87 125 my $regex;
88 125 ▶ if (/ to (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ to (?:the )?(?:$ord )?mixing bowl$/) { } 3 122 125
89 3 $regex = qr/^Add (?:the )?$ingr to (?:the )?(?:$ord )?mixing bowl$/;
90 } else {
91 122 $regex = qr/^Add (?:the )?$ingr()$/;
92 }
93 125 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 3 122 125
94 3 ▶ $recipe->require_bowl($2||1);
Condition50%1/2
$2 || 1
l!l
30
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
95 3 $recipe->require_ingredient($1, 'add');
96 3 ▶ return 'add', $1, ($2||1);
Condition50%1/2
$2 || 1
l!l
30
MC/DC0%0/1
$2 || 1
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
97 },
98  
99 122 ▶ remove => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
100 122 my $recipe = shift;
101 122 local $_ = shift;
102 122 my $regex;
103 122 ▶ if (/ from (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ from (?:the )?(?:$ord )?mixing bowl$/) { } 55 67 122
104 55 $regex = qr/^Remove (?:the )?$ingr from (?:the )?(?:$ord )?mixing bowl$/;
105 } else {
106 67 $regex = qr/^Remove (?:the )?$ingr()$/;
107 }
108 122 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 56 66 122
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 66 ▶ combine => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
115 66 my $recipe = shift;
116 66 local $_ = shift;
117 66 my $regex;
118 66 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 8 58 66
119 8 $regex = qr/^Combine (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
120 } else {
121 58 $regex = qr/^Combine (?:the )?$ingr()$/;
122 }
123 66 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 4 62 66
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 62 ▶ divide => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
130 62 my $recipe = shift;
131 62 local $_ = shift;
132 62 my $regex;
133 62 ▶ if (/ into (?:the )?(?:$ord )?mixing bowl$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ into (?:the )?(?:$ord )?mixing bowl$/) { } 6 56 62
134 6 $regex = qr/^Divide (?:the )?$ingr into (?:the )?(?:$ord )?mixing bowl$/;
135 } else {
136 56 $regex = qr/^Divide(?: the)?$ingr()$/;
137 }
138 62 ▶ /$regex/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/$regex/ or return () 4 58 62
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 211 ▶ add_dry => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
145 211 my $recipe = shift;
146 211 local $_ = shift;
147 211 ▶ /^Add (?:the )?dry ingredients(?: to (?:the )?(?:$ord )?mixing bowl)?$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Add (?:the )?dry ingredients(?: to (?:the )?(?:$ord )?mixing bowl)?$/ or return () 4 207 211
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 58 ▶ liquify_contents => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
153 58 my $recipe = shift;
154 58 local $_ = shift;
155 58 ▶ /^Liqu(?:i|e)fy (?:the )?contents of (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Liqu(?:i|e)fy (?:the )?contents of (?:the )?(?:$ord )?mixing bowl$/ or return () 11 47 58
156 11 ▶ $recipe->require_bowl($1||1);
Condition100%2/2
$1 || 1
l!l
83
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
157 11 ▶ return 'liquify_contents', ($1||1);
Condition100%2/2
$1 || 1
l!l
83
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
158 },
159  
160 47 ▶ liquify => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
161 47 my $recipe = shift;
162 47 local $_ = shift;
163 47 ▶ /^Liqu(?:i|e)fy (?:the )?$ingr$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Liqu(?:i|e)fy (?:the )?$ingr$/ or return () 1 46 47
164 1 $recipe->require_ingredient($1, 'liquify');
165 1 return 'liquify', $1;
166 },
167  
168 46 ▶ stir_time => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
169 46 my $recipe = shift;
170 46 local $_ = shift;
171 46 ▶ /^Stir (?:(?:the )?(?:$ord )?mixing bowl )?for (\d+) minutes?$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Stir (?:(?:the )?(?:$ord )?mixing bowl )?for (\d+) minutes?$/ or return () 3 43 46
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 43 ▶ stir_ingredient => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
177 43 my $recipe = shift;
178 43 local $_ = shift;
179 43 ▶ /^Stir $ingr into (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Stir $ingr into (?:the )?(?:$ord )?mixing bowl$/ or return () 2 41 43
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 41 ▶ mix => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
186 41 my $recipe = shift;
187 41 local $_ = shift;
188 41 ▶ /^Mix (?:the (?:$ord )?mixing bowl )well$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Mix (?:the (?:$ord )?mixing bowl )well$/ or return () 1 40 41
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 40 ▶ clean => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
194 40 my $recipe = shift;
195 40 local $_ = shift;
196 40 ▶ /^Clean (?:the )?(?:$ord )?mixing bowl$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Clean (?:the )?(?:$ord )?mixing bowl$/ or return () 3 37 40
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 37 ▶ pour => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
202 37 my $recipe = shift;
203 37 local $_ = shift;
204 37 ▶ /^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 () 22 15 37
205 22 ▶ my $m = $1 || 1;
Condition100%2/2
$1 || 1
l!l
148
MC/DC100%1/1
$1 || 1
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
206 22 ▶ my $b = $2 || 1;
Condition100%2/2
$2 || 1
l!l
1012
MC/DC100%1/1
$2 || 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
207 22 $m =~ s/\D//g;
208 22 $b =~ s/\D//g;
209 22 $recipe->require_bowl($m);
210 22 $recipe->require_dish($b);
211 22 return 'pour', $m, $b;
212 },
213  
214 15 ▶ refrigerate => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
215 15 my $recipe = shift;
216 15 local $_ = shift;
217 15 ▶ /^Refrigerate(?: for (\d+) hours?)?$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Refrigerate(?: for (\d+) hours?)?$/ or return () 4 11 15
218 4 ▶ return 'refrigerate', (defined $1 ? $1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 3 1 4
219 },
220  
221 11 ▶ set_aside => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
222 11 my $recipe = shift;
223 11 local $_ = shift;
224 11 ▶ /^Set aside$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Set aside$/ or return () 0 11 11
225 0 return 'set_aside';
226 },
227  
228 11 ▶ serve_with => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
229 11 my $recipe = shift;
230 11 local $_ = shift;
231 11 ▶ /^Serve with $ingr$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^Serve with $ingr$/ or return () 1 10 11
232 # $ingr is a recipe name here
233 1 return 'serve_with', lc($1);
234 },
235  
236 5 ▶ verb => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
237 5 my $recipe = shift;
238 5 local $_ = shift;
239 5 ▶ /^$verb (?:the )?$ingr$/ or return();
Branch: 1 branch
BranchTrueFalseTotal
/^$verb (?:the )?$ingr$/ or return () 5 0 5
240 5 $recipe->require_ingredient($2, 'verb');
241 5 return 'verb', lc($1), $2;
242 },
243  
244 10 ▶ until_verbed => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
245 10 my $recipe = shift;
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 10 );
258  
259 }
260  
261 %Commands = (
262 659 ▶ put => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
263 659 my $recipe = shift;
264 659 my $data = shift;
265 $recipe -> {bowls} -> [$data -> [2] - 1]
266 ->
267 659 put( $recipe -> {ingredients} -> {$data -> [1]} );
268 659 return 1;
269 },
270
271 2 ▶ take_from => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
272 2 my $recipe = shift;
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 111 ▶ fold => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
285 111 my $recipe = shift;
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 3 ▶ add => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
294 3 my $recipe = shift;
295 3 my $data = shift;
296 $recipe -> {bowls} -> [$data -> [2] - 1]
297 ->
298 3 add( $recipe -> {ingredients} -> {$data -> [1]} );
299 3 return 1;
300 },
301  
302 168 ▶ remove => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
303 168 my $recipe = shift;
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 240 ▶ combine => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
312 240 my $recipe = shift;
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 0 ▶ divide => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 0 my $recipe = shift;
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 0 ▶ add_dry => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
330 0 my $recipe = shift;
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 0 ▶ liquify => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
342 0 my $recipe = shift;
343 0 my $data = shift;
344 0 $recipe -> {ingredients} -> {$data -> [1]} -> liquify();
345 0 return 1;
346 },
347  
348 21 ▶ liquify_contents => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
349 21 my $recipe = shift;
350 21 my $data = shift;
351 21 $recipe -> {bowls} -> [$data -> [1] - 1] -> liquify_contents();
352 21 return 1;
353 },
354  
355 0 ▶ stir_time => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 my $recipe = shift;
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 0 ▶ stir_ingredient => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
365 0 my $recipe = shift;
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 0 ▶ mix => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
374 0 my $recipe = shift;
375 0 my $data = shift;
376 0 $recipe -> {bowls} -> [$data -> [1] - 1] -> mix();
377 0 return 1;
378 },
379  
380 72 ▶ clean => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
381 72 my $recipe = shift;
382 72 my $data = shift;
383 72 $recipe -> {bowls} -> [$data -> [1] - 1] -> clean();
384 72 return 1;
385 },
386  
387 95 ▶ pour => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
388 95 my $recipe = shift;
389 95 my $data = shift;
390 95 my @stuff = $recipe -> {bowls} -> [$data -> [1] - 1] -> pour();
391  
392 95 $recipe -> {dishes} -> [$data -> [2] - 1] -> put( $_ ) foreach @stuff;
393  
394 95 return 1;
395 },
396  
397 8 ▶ refrigerate => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
398 8 my $recipe = shift;
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 0 ▶ set_aside => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
409 0 my $recipe = shift;
410 0 my $data = shift;
411  
412 0 return 'break';
413 },
414  
415 36 ▶ serve_with => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
416 36 my $recipe = shift;
417 36 my $data = shift;
418  
419 36 my $rec_recipe = $data->[1];
420  
421 36 return "recurse.$rec_recipe" ;
422 },
423  
424 624 ▶ verb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
425 624 my $recipe = shift;
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 576 ▶ until_verbed => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
434 576 my $recipe = shift;
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 10 );
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 108 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
458 108 my $proto = shift;
459 108 ▶ my $class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
98100
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 108 my $self = {};
462  
463 108 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 98 10 108
464 98 %$self = %$proto;
465  
466 98 $self->{bowls} = [ map { $_->new() } @{$self -> {bowls }} ];
467 98 $self->{dishes} = [ map { $_->new() } @{$self -> {dishes}} ];
468 84 $self->{loops} = { map { ( $_, $self->{loops}{$_} ) }
469 98 keys %{$self->{loops}} };
470  
471 98 ▶ if ( $self->{compiled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"compiled"}) 98 0 98
472 $self->{ingredients} = { map {
473 (
474 $_,
475 341 $self -> {ingredients} -> {$_} -> new()
476 )
477 98 } keys %{ $self->{ingredients} }
478 };
479 }
480 }
481  
482 108 my %args = @_;
483  
484 108 %$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 108 bless $self => $class;
502 108 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 62 ▶sub execute {
Subroutine execute: called CC=13 SCAR=25.8
Pod documented
515 62 my $self = shift;
516  
517 62 my $recipes = shift;
518  
519 62 ▶ $self->compile() unless $self->{compiled};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"compiled"} 0 62 62
520  
521 62 my @loop_stack;
522  
523 62 my $max_pos = $#{$self->{method}};
524 62 my $exec_pos = 0;
525 62 while (1) {
526  
527 2615 my $next_method = $self->{method}->[$exec_pos];
528 # print ' ' x scalar(@loop_stack), join(',', @$next_method),"\n";
529  
530 2615 my $return = $Commands{$next_method->[0]}->($self, $next_method);
531  
532 2615 ▶ last if $return eq 'halt';
Branch: 1 branch
BranchTrueFalseTotal
if $return eq "halt" 8 2607 2615
533  
534 2607 ▶ if ( $return =~ /^recurse\.([\-\w][\-\w ]*)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($return =~ /^recurse\.([\-\w][\-\w ]*)/) { } 36 2571 2607
elsif ($return =~ /^loop\.([^\.]+)\.([^\.]+)/) { } 624 1947 2571
elsif ($return =~ /^endloop\.([^\.]+)/) { } 576 1371 1947
elsif ($return =~ /^break/) { } 0 1371 1371
535 36 ▶ exists $recipes->{$1}
Branch: 1 branch
BranchTrueFalseTotal
exists $recipes->{$1} or croak("Invalid recipe '$1' specified for recursion.") 36 0 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 2607 $exec_pos++;
573 2607 ▶ last if $exec_pos > $max_pos;
Branch: 1 branch
BranchTrueFalseTotal
if $exec_pos > $max_pos 54 2553 2607
574 }
575  
576 62 ▶ if ( $self->{serves} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"serves"}) 26 36 62
577 26 foreach my $serve ( 0..($self->{serves}-1) ) {
578 35 ▶ last if $serve > $#{$self->{dishes}};
Branch: 1 branch
BranchTrueFalseTotal
if $serve > $#{$$self{"dishes"};} 0 35 35
579 35 my $string = $self->{dishes}->[$serve]->print();
580 35 $self->{output} .= $string;
581 }
582 }
583  
584 62 return $self;
585 }
586  
587 =item first_bowl
588  
589 Returns the first bowl of the recipe.
590  
591 =cut
592  
593 36 ▶sub first_bowl {
Subroutine first_bowl: called CC=1 SCAR=0.0
Pod documented
594 36 my $self = shift;
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 162 ▶sub require_ingredient {
Subroutine require_ingredient: called CC=5 SCAR=20.9
Pod documented
607 162 my $self = shift;
608 162 my $ingredient = shift;
609 162 my $sub = shift;
610  
611 162 ▶ (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}'" : "") . '.') 162 0 162
defined $ingredient ? : 0 0 0
defined $sub ? : 0 0 0
Condition33%1/3
defined $ingredient and exists $self->{"ingredients"}{$ingredient}
!ll&&!rl&&r
00162
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 162 return $self;
617 }
618  
619 =item output
620  
621 Mutator for the Recipe output.
622  
623 =cut
624  
625 98 ▶sub output {
Subroutine output: called CC=2 SCAR=6.9
Pod documented
626 98 my $self = shift;
627  
628 98 ▶ $self->{output} .= shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 36 62 98
629  
630 98 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 195 ▶sub require_bowl {
Subroutine require_bowl: called CC=3 SCAR=11.0
Pod documented
641 195 my $self = shift;
642 195 my $no = shift;
643  
644 195 ▶ return if @{$self->{bowls}} >= $no;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"bowls"};} >= $no 172 23 195
645  
646 23 ▶ while (@{$self->{bowls}} < $no) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"bowls"};} < $no) 23 23 46
647 23 push @{$self->{bowls}}, Acme::Chef::Container->new();
648 }
649  
650 23 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 22 ▶sub require_dish {
Subroutine require_dish: called CC=3 SCAR=11.0
Pod documented
662 22 my $self = shift;
663 22 my $no = shift;
664  
665 22 ▶ return if @{$self->{dishes}} >= $no;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"dishes"};} >= $no 10 12 22
666  
667 12 ▶ while (@{$self->{dishes}} < $no) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"dishes"};} < $no) 12 12 24
668 12 push @{$self->{dishes}}, Acme::Chef::Container->new();
669 }
670  
671 12 return $self;
672 }
673  
674 =item recipe_name
675  
676 Mutator for the recipe name.
677  
678 =cut
679  
680 19 ▶sub recipe_name {
Subroutine recipe_name: called CC=2 SCAR=7.1
Pod documented
681 19 my $self = shift;
682  
683 19 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 19 19
684  
685 19 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 10 ▶sub compile {
Subroutine compile: called CC=19 SCAR=29.8
Pod documented
697 10 my $self = shift;
698  
699 10 ▶ return 0 if $self->{compiled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"compiled"} 0 10 10
700  
701 10 my @ingredients = split /\n/, $self->{ingredients};
702  
703 10 shift @ingredients; # remove header line
704  
705 10 ▶ @ingredients or croak "Failed compiling recipe. No ingredients specified.";
Branch: 1 branch
BranchTrueFalseTotal
@ingredients or croak("Failed compiling recipe. No ingredients specified.") 10 0 10
706  
707 10 my %ingredients;
708 10 my $ingredient_no = 0;
709  
710 10 foreach (@ingredients) {
711 56 $ingredient_no++;
712  
713 56 my $value;
714 56 ▶ if (s/^[ ]*(\d+)[ ]//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^[ ]*(\d+)[ ]//) { } 55 1 56
715 55 $value = $1;
716 } else {
717 1 $value = undef;
718 }
719  
720 56 my $measure_type = '';
721 56 foreach my $type ( keys %Acme::Chef::Ingredient::MeasureTypes ) {
722 112 ▶ if ( s/^\Q$type\E[ ]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\Q$type\E[ ]//) 0 112 112
723 0 $measure_type = $type;
724 0 last;
725 }
726 }
727  
728 56 my $measure = '';
729 56 foreach my $meas ( keys %Acme::Chef::Ingredient::Measures ) {
730 657 ▶ next if $meas eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $meas eq "" 52 605 657
731  
732 605 ▶ if ( s/^\Q$meas\E[ ]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\Q$meas\E[ ]//) 23 582 605
733 23 $measure = $meas;
734 23 last;
735 }
736 }
737  
738 /[ ]*([\-\w][\- \w]*)[ ]*$/
739 56 ▶ 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).") 56 0 56
740  
741 56 my $ingredient_name = $1;
742  
743 56 my $ingredient = Acme::Chef::Ingredient->new(
744 name => $ingredient_name,
745 value => $value,
746 measure => $measure,
747 measure_type => $measure_type,
748 );
749  
750 56 $ingredients{$ingredient_name} = $ingredient;
751 }
752  
753 10 $self->{ingredients} = \%ingredients;
754  
755 10 $self->{method} =~ s/\s+/ /g;
756  
757 10 my @steps = split /\s*\.\s*/, $self->{method};
758  
759 10 shift @steps; # remove "Method."
760  
761 10 my $step_no = 0;
762 10 foreach my $step (@steps) {
763 212 $step_no++;
764  
765 212 foreach my $grammar (@GrammarOrder) {
766 1498 my @res = $Grammars{$grammar}->($self, $step);
767 1498 ▶ @res or next;
Branch: 1 branch
BranchTrueFalseTotal
@res or next 212 1286 1498
768  
769 212 ▶ if ( $res[0] eq 'verb' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res[0] eq 'verb') { } 5 207 212
elsif ($res[0] eq 'until_verbed') { } 5 202 207
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}'.") 5 0 5
777 or croak "Loop end without loop start '$verb'.";
778  
779 5 $self->{loops}->{$verb}->{end} = $step_no - 1;
780 }
781  
782 212 $step = [@res];
783 212 last;
784 }
785  
786 212 ▶ croak "Invalid method step (step no. $step_no): '$step'."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $step eq "ARRAY" 0 212 212
787 if not ref $step eq 'ARRAY';
788 }
789  
790 10 ▶ 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 10 10
791 0 croak "Not all loop starting points have matching ends.";
792 }
793  
794 10 $self->{method} = \@steps;
795  
796 10 $self->{compiled} = 1;
797  
798 10 return $self;
799 }
800  
801  
802 __END__
« Chef.pm ↑ Summary Chef/Ingredient.pm »