| 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 | 10 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use vars qw/$VERSION/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | $VERSION = '1.03'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use Acme::Chef::Recipe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use Acme::Chef::Container; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 10 | ▶ | 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 | 9 | ▶ | sub compile { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 9 | my $proto = shift; | |||||||||||||||||||||||||||
| 76 | 9 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 9 | my $code = shift; | |||||||||||||||||||||||||||
| 79 | 9 | ▶ | defined $code or croak "compile takes one argument: a code string."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 9 | my $self = {}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 9 | bless $self => $class; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 9 | my @paragraphs = $self->_get_paragraphs( $code ); | |||||||||||||||||||||||||||
| 86 | 9 | my @recipes = $self->_paragraphsToRecipes(\@paragraphs); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 9 | $_->compile() foreach @recipes; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 9 | $self->{start_recipe} = $recipes[0]->recipe_name(); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | $self->{recipes} = { | ||||||||||||||||||||||||||||
| 93 | 9 | map { ($_->recipe_name(), $_) } @recipes | |||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 9 | 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 | 26 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 26 | my $start_recipe = $self->{recipes}->{ $self->{start_recipe} }->new(); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 26 | $start_recipe->execute($self->{recipes}); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 26 | 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 | 18 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 18 | my $type = shift; | |||||||||||||||||||||||||||
| 132 | 18 | ▶ | $type = '' if not defined $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 18 | local $@ = undef; | |||||||||||||||||||||||||||
| 135 | 18 | require Data::Dumper; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 18 | my $dumper = Data::Dumper->new([$self], ['self']); | |||||||||||||||||||||||||||
| 138 | 18 | $dumper->Indent(0); | |||||||||||||||||||||||||||
| 139 | 18 | $dumper->Purity(1); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 18 | my $dump = $dumper->Dump(); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 18 | ▶ | if ($type =~ /^autorun$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 9 | $dump = 'do{my ' . $dump . ' bless $self => "' . (__PACKAGE__) . '"; $self->execute();} '; | |||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||
| 146 | 9 | $dump = 'do{my ' . $dump . ' bless $self => "' . (__PACKAGE__) . '";} '; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 18 | return $dump; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # private function _get_paragraphs | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 9 | ▶ | sub _get_paragraphs { | ||||||||||||||||||||||||||
|
Subroutine
_get_paragraphs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 9 | my $string = shift; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 9 | $string =~ s/^\s+//; | |||||||||||||||||||||||||||
| 161 | 9 | $string =~ s/\s+$//; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 9 | return split /\n{2,}/, $string; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # private function _paragraphsToRecipes | ||||||||||||||||||||||||||||
| 168 | # | ||||||||||||||||||||||||||||
| 169 | # Constructs recipes from an array ref of paragraphs. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 9 | ▶ | sub _paragraphsToRecipes { | ||||||||||||||||||||||||||
|
Subroutine
_paragraphsToRecipes: called CC=18 SCAR=34.0
| |||||||||||||||||||||||||||||
| 172 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 9 | my $paragraphs = shift; | |||||||||||||||||||||||||||
| 175 | 9 | ▶ | $paragraphs = shift if not defined $paragraphs or not ref $paragraphs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $paragraphs and ref $paragraphs
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
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 9 | ▶ | while (chomp @$paragraphs) {} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 9 | my @recipes; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 9 | my $paragraph_no = 0; | |||||||||||||||||||||||||||
| 182 | 9 | ▶ | while (@$paragraphs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 10 | my $recipe_name = shift @$paragraphs; | |||||||||||||||||||||||||||
| 184 | 10 | $paragraph_no++; | |||||||||||||||||||||||||||
| 185 | 10 | ▶ | $recipe_name =~ /^[ ]*([\-\w][\- \w]*)\.[ ]*$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | or croak "Invalid recipe name specifier in paragraph no. $paragraph_no."; | ||||||||||||||||||||||||||||
| 187 | 10 | $recipe_name = lc($1); | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 10 | ▶ | last unless @$paragraphs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 10 | my $comments = shift @$paragraphs; | |||||||||||||||||||||||||||
| 191 | 10 | $paragraph_no++; | |||||||||||||||||||||||||||
| 192 | 10 | my $ingredients; | |||||||||||||||||||||||||||
| 193 | 10 | ▶ | if ( $comments =~ /^[ ]*Ingredients\.[ ]*\n/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 5 | $ingredients = $comments; | |||||||||||||||||||||||||||
| 195 | 5 | $comments = ''; | |||||||||||||||||||||||||||
| 196 | } else { | ||||||||||||||||||||||||||||
| 197 | 5 | ▶ | last unless @$paragraphs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 5 | $ingredients = shift @$paragraphs; | |||||||||||||||||||||||||||
| 199 | 5 | $paragraph_no++; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 10 | ▶ | last unless @$paragraphs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 10 | my $cooking_time = shift @$paragraphs; | |||||||||||||||||||||||||||
| 204 | 10 | $paragraph_no++; | |||||||||||||||||||||||||||
| 205 | 10 | my $temperature; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 10 | ▶ | if ($cooking_time =~ /^[ ]*Cooking time:[ ]*(\d+)(?: hours?| minutes?)\.[ ]*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | $cooking_time = $1; | |||||||||||||||||||||||||||
| 209 | 0 | ▶ | last unless @$paragraphs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $temperature = shift @$paragraphs; | |||||||||||||||||||||||||||
| 211 | 0 | $paragraph_no++; | |||||||||||||||||||||||||||
| 212 | } else { | ||||||||||||||||||||||||||||
| 213 | 10 | $temperature = $cooking_time; | |||||||||||||||||||||||||||
| 214 | 10 | $cooking_time = ''; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 10 | my $method; | |||||||||||||||||||||||||||
| 218 | 10 | ▶ | if ($temperature =~ /^[ ]*Pre-heat oven to (\d+) degrees Celsius(?: gas mark (\d+))?\.[ ]*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | $temperature = $1; | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | $temperature .= ",$2" if defined $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | last unless @$paragraphs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | $method = shift @$paragraphs; | |||||||||||||||||||||||||||
| 223 | 0 | $paragraph_no++; | |||||||||||||||||||||||||||
| 224 | } else { | ||||||||||||||||||||||||||||
| 225 | 10 | $method = $temperature; | |||||||||||||||||||||||||||
| 226 | 10 | $temperature = ''; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 10 | ▶ | $method =~ /^[ ]*Method\.[ ]*\n/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | or croak "Invalid method specifier in paragraph no. $paragraph_no."; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 10 | my $serves = ''; | |||||||||||||||||||||||||||
| 233 | 10 | ▶ | if (@$paragraphs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 7 | $serves = shift @$paragraphs; | |||||||||||||||||||||||||||
| 235 | 7 | ▶ | if ($serves =~ /^[ ]*Serves (\d+)\.[ ]*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 7 | $serves = $1; | |||||||||||||||||||||||||||
| 237 | 7 | $paragraph_no++; | |||||||||||||||||||||||||||
| 238 | } else { | ||||||||||||||||||||||||||||
| 239 | 0 | unshift @$paragraphs, $serves; | |||||||||||||||||||||||||||
| 240 | 0 | $serves = ''; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 10 | 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 | 9 | return @recipes; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 10 | 1; | |||||||||||||||||||||||||||
| 261 | __END__ | ||||||||||||||||||||||||||||