blib/lib/Acme/Chef.pm

filter: Statementstmt 90.1% 101 / 112 Branchbran 55.2% 21 / 38 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 79.5% 140 / 176 CC 23 SCAR 28.0
CC
23
Cov
79%
_paragraphsToRecipes
30.0
compile
4.1
dump
3.0
CRAP 16.4
↑ Summary blib/lib/Acme/Chef/Recipe.pm »
1 # Please read the pod documentation in this file for
2 # details on how to reach the author and copyright issues.
3  
4 package Acme::Chef;
5  
6 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 $VERSION = '1.01';
14  
15 7 ▶use Acme::Chef::Recipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Acme::Chef::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Acme::Chef::Ingredient;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Acme::Chef - An interpreter for the Chef programming language
22  
23 =head1 SYNOPSIS
24  
25 # Using the script that comes with the distribution.
26 chef.pl file.chef
27
28 # Using the module
29 use Acme::Chef;
30
31 my $compiled = Acme::Chef->compile($code_string);
32 print $compiled->execute();
33
34 my $string = $compiled->dump(); # requires Data::Dumper
35 # Save it to disk, send it over the web, whatever.
36 my $reconstructed_object = eval $string;
37
38 # or:
39 $string = $compiled->dump('autorun'); # requires Data::Dumper
40 # Save it to disk, send it over the web, whatever.
41 my $output_of_chef_program = eval $string;
42  
43 =head1 DESCRIPTION
44  
45 Chef is an esoteric programming language in which programs look like
46 recipes. I needn't mention that using it in
47 production environment, heck, using it for anything but entertainment
48 ought to result in bugs and chaos in reverse order.
49  
50 All methods provided by Acme::Chef are adequately described in the
51 synopsis. If you don't think so, you need to read the source code.
52  
53 There has been an update to the Chef specification. I have implemented
54 the changes and marked them in the following documentation with
55 "I<new specification>".
56  
57 With that out of the way, I would like to present a pod-formatted
58 copy of the Chef specification from David Morgan-Mar's homepage
59 (L<http://www.dangermouse.net/esoteric/chef.html>).
60  
61 =head2 METHODS
62  
63 This is a list of methods in this package.
64  
65 =over 2
66  
67 =item compile
68  
69 Takes Chef source code as first argument and compiles a Chef program from it.
70 This method doesn't run the code, but returns a program object.
71  
72 =cut
73  
74 sub compile {
75 6 ▶ my $proto = shift;
Subroutine compile: called CC=4 SCAR=14.0
Pod documented
76 6 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
77  
78 6 my $code = shift;
79 6 ▶ defined $code or croak "compile takes one argument: a code string.";
Branch: 1 branch
BranchTrueFalseTotal
defined $code or croak("compile takes one argument: a code string.") 0 6 6
80  
81 6 my $self = {};
82  
83 6 bless $self => $class;
84  
85 6 my @paragraphs = $self->_get_paragraphs( $code );
86 6 my @recipes = $self->_paragraphsToRecipes(\@paragraphs);
87  
88 6 $_->compile() foreach @recipes;
89  
90 6 $self->{start_recipe} = $recipes[0]->recipe_name();
91  
92 $self->{recipes} = {
93 6 map { ($_->recipe_name(), $_) } @recipes
94 };
95  
96 6 return $self;
97 }
98  
99  
100 =item execute
101
102 Takes no arguments. Runs the program and returns its output.
103  
104 =cut
105  
106 sub execute {
107 17 ▶ my $self = shift;
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
108  
109 17 my $start_recipe = $self->{recipes}->{ $self->{start_recipe} }->new();
110  
111 17 $start_recipe->execute($self->{recipes});
112  
113 17 return $start_recipe->output();
114 }
115  
116  
117 =item dump
118
119 Takes one optional argument. If it equals 'autorun',
120 dump returns a string that, when evaluated, executes
121 the program and returns the output.
122  
123 If the argument does not equal 'autorun', a different
124 string is returned that reconstructs the Acme::Chef
125 object.
126  
127 =cut
128  
129 sub dump {
130 12 ▶ my $self = shift;
Subroutine dump: called CC=3 SCAR=11.0
Pod documented
131 12 my $type = shift;
132 12 ▶ $type = '' if not defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 6 6 12
133  
134 12 local $@ = undef;
135 12 require Data::Dumper;
136  
137 12 my $dumper = Data::Dumper->new([$self], ['self']);
138 12 $dumper->Indent(0);
139 12 $dumper->Purity(1);
140  
141 12 my $dump = $dumper->Dump();
142  
143 12 ▶ if ($type =~ /^autorun$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^autorun$/) { } 6 6 12
144 6 $dump = 'do{my ' . $dump . ' bless $self => "' . (__PACKAGE__) . '"; $self->execute();} ';
145 } else {
146 6 $dump = 'do{my ' . $dump . ' bless $self => "' . (__PACKAGE__) . '";} ';
147 }
148  
149 12 return $dump;
150 }
151  
152  
153 # private function _get_paragraphs
154  
155 sub _get_paragraphs {
156 6 ▶ my $self = shift;
Subroutine _get_paragraphs: called CC=1 SCAR=0.0
157  
158 6 my $string = shift;
159  
160 6 $string =~ s/^\s+//;
161 6 $string =~ s/\s+$//;
162  
163 6 return split /\n{2,}/, $string;
164 }
165  
166  
167 # private function _paragraphsToRecipes
168 #
169 # Constructs recipes from an array ref of paragraphs.
170  
171 sub _paragraphsToRecipes {
172 6 ▶ my $self = shift;
Subroutine _paragraphsToRecipes: called CC=18 SCAR=34.0
173  
174 6 my $paragraphs = shift;
175 6 ▶ $paragraphs = shift if not defined $paragraphs or not ref $paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $paragraphs and ref $paragraphs 0 6 6
Condition33%1/3
defined $paragraphs and ref $paragraphs
!ll&&!rl&&r
006
MC/DC0%0/2
defined $paragraphs and ref $paragraphs
defined $paragraphs ref $paragraphs
Truth table33%1/3
A and B
A= defined $paragraphsB= ref $paragraphs
ABresult
0X0
100
111
176  
177 6 ▶ while (chomp @$paragraphs) {}
Branch: 1 branch
BranchTrueFalseTotal
if (chomp @$paragraphs) 0 6 6
178  
179 6 my @recipes;
180  
181 6 my $paragraph_no = 0;
182 6 ▶ while (@$paragraphs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$paragraphs) 7 6 13
183 7 my $recipe_name = shift @$paragraphs;
184 7 $paragraph_no++;
185 7 ▶ $recipe_name =~ /^[ ]*([\-\w][\- \w]*)\.[ ]*$/
Branch: 1 branch
BranchTrueFalseTotal
$recipe_name =~ /^[ ]*([\-\w][\- \w]*)\.[ ]*$/ or croak("Invalid recipe name specifier in paragraph no. $paragraph_no.") 0 7 7
186 or croak "Invalid recipe name specifier in paragraph no. $paragraph_no.";
187 7 $recipe_name = lc($1);
188  
189 7 ▶ last unless @$paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paragraphs 0 7 7
190 7 my $comments = shift @$paragraphs;
191 7 $paragraph_no++;
192 7 my $ingredients;
193 7 ▶ if ( $comments =~ /^[ ]*Ingredients\.[ ]*\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comments =~ /^[ ]*Ingredients\.[ ]*\n/) { } 4 3 7
194 4 $ingredients = $comments;
195 4 $comments = '';
196 } else {
197 3 ▶ last unless @$paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paragraphs 0 3 3
198 3 $ingredients = shift @$paragraphs;
199 3 $paragraph_no++;
200 }
201  
202 7 ▶ last unless @$paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paragraphs 0 7 7
203 7 my $cooking_time = shift @$paragraphs;
204 7 $paragraph_no++;
205 7 my $temperature;
206  
207 7 ▶ if ($cooking_time =~ /^[ ]*Cooking time:[ ]*(\d+)(?: hours?| minutes?)\.[ ]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cooking_time =~ /^[ ]*Cooking time:[ ]*(\d+)(?: hours?| minutes?)\.[ ]*$/) { } 0 7 7
208 0 $cooking_time = $1;
209 0 ▶ last unless @$paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paragraphs 0 0 0
210 0 $temperature = shift @$paragraphs;
211 0 $paragraph_no++;
212 } else {
213 7 $temperature = $cooking_time;
214 7 $cooking_time = '';
215 }
216  
217 7 my $method;
218 7 ▶ if ($temperature =~ /^[ ]*Pre-heat oven to (\d+) degrees Celsius(?: gas mark (\d+))?\.[ ]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temperature =~ /^[ ]*Pre-heat oven to (\d+) degrees Celsius(?: gas mark (\d+))?\.[ ]*$/) { } 0 7 7
219 0 $temperature = $1;
220 0 ▶ $temperature .= ",$2" if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
221 0 ▶ last unless @$paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paragraphs 0 0 0
222 0 $method = shift @$paragraphs;
223 0 $paragraph_no++;
224 } else {
225 7 $method = $temperature;
226 7 $temperature = '';
227 }
228  
229 7 ▶ $method =~ /^[ ]*Method\.[ ]*\n/
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /^[ ]*Method\.[ ]*\n/ or croak("Invalid method specifier in paragraph no. $paragraph_no.") 0 7 7
230 or croak "Invalid method specifier in paragraph no. $paragraph_no.";
231
232 7 my $serves = '';
233 7 ▶ if (@$paragraphs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$paragraphs) 4 3 7
234 4 $serves = shift @$paragraphs;
235 4 ▶ if ($serves =~ /^[ ]*Serves (\d+)\.[ ]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serves =~ /^[ ]*Serves (\d+)\.[ ]*$/) { } 4 0 4
236 4 $serves = $1;
237 4 $paragraph_no++;
238 } else {
239 0 unshift @$paragraphs, $serves;
240 0 $serves = '';
241 }
242 }
243  
244 7 push @recipes, Acme::Chef::Recipe->new(
245 name => $recipe_name,
246 comments => $comments,
247 ingredients => $ingredients,
248 cooking_time => $cooking_time,
249 temperature => $temperature,
250 method => $method,
251 serves => $serves,
252 );
253
254 }
255
256 6 return @recipes;
257 }
258  
259  
260 7 1;
261 __END__
↑ Summary blib/lib/Acme/Chef/Recipe.pm »