Acme-Chef
view release on metacpan or search on metacpan
lib/Acme/Chef/Recipe.pm view on Meta::CPAN
},
set_aside => sub {
my $recipe = shift;
my $data = shift;
return 'break';
},
serve_with => sub {
my $recipe = shift;
my $data = shift;
my $rec_recipe = $data->[1];
return "recurse.$rec_recipe" ;
},
verb => sub {
my $recipe = shift;
my $data = shift;
my $verb = $data->[1];
my $ingr = $data->[2];
return "loop.$verb.$ingr";
},
until_verbed => sub {
my $recipe = shift;
my $data = shift;
my $verb = $data->[1];
if ( exists $recipe->{ingredients}->{$data->[2]} ) {
my $ingr = $recipe->{ingredients}->{$data->[2]};
$ingr->value( $ingr->value() - 1 );
}
return "endloop.$verb";
},
);
=item new
Acme::Chef::Recipe constructor. Arguments are interpreted as key/value pairs
and used as object attributes.
=cut
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
my $self = {};
if (ref $proto) {
%$self = %$proto;
$self->{bowls} = [ map { $_->new() } @{$self -> {bowls }} ];
$self->{dishes} = [ map { $_->new() } @{$self -> {dishes}} ];
$self->{loops} = { map { ( $_, $self->{loops}{$_} ) }
keys %{$self->{loops}} };
if ( $self->{compiled} ) {
$self->{ingredients} = { map {
(
$_,
$self -> {ingredients} -> {$_} -> new()
)
} keys %{ $self->{ingredients} }
};
}
}
my %args = @_;
%$self = (
compiled => 0,
name => '',
comments => '',
ingredients => '',
cooking_time => '',
temperature => '',
method => '',
serves => '',
output => '',
loops => {},
bowls => [],
dishes => [],
%$self,
%args,
);
bless $self => $class;
return $self;
}
=item execute
Executes the recipe (program). First argument should be a reference to a
hash of sous-recipes.
=cut
sub execute {
my $self = shift;
my $recipes = shift;
$self->compile() unless $self->{compiled};
my @loop_stack;
my $max_pos = $#{$self->{method}};
my $exec_pos = 0;
while (1) {
my $next_method = $self->{method}->[$exec_pos];
# print ' ' x scalar(@loop_stack), join(',', @$next_method),"\n";
my $return = $Commands{$next_method->[0]}->($self, $next_method);
last if $return eq 'halt';
( run in 2.680 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )