From: Matt S Trout Date: Mon, 20 Jan 2014 12:39:08 +0000 (+0000) Subject: add but method to state X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FDKit.git;a=commitdiff_plain;h=030795107c8704c23e0a4dc483d68ee291a1570f add but method to state --- diff --git a/lib/DX/State.pm b/lib/DX/State.pm index 4815ab2..2bf0f07 100644 --- a/lib/DX/State.pm +++ b/lib/DX/State.pm @@ -23,7 +23,7 @@ sub bind_stream_then { my ($self, $var, $stream, $then) = @_; warn "Binding ".$var->id." to $stream"; my $bound = $var->with_stream($stream); - $self->new(%$self, + $self->but( by_id => { %{$self->by_id}, $var->id => $bound }, next_op => $then )->mark_choice($bound); @@ -31,9 +31,7 @@ sub bind_stream_then { sub mark_choice { my ($self, $var) = @_; - $self->new(%$self, - last_choice => [ $self, $var ] - ); + $self->but(last_choice => [ $self, $var ]); } sub backtrack { @@ -51,7 +49,7 @@ sub backtrack { sub then { my ($self, $then) = @_; - $self->new(%$self, next_op => $then); + $self->but(next_op => $then); } sub run { @@ -70,4 +68,9 @@ sub push_backtrack { $_[0]->then(DX::Op::FromCode->new(code => sub { $_[1]->backtrack })); } +sub but { + my ($self, @but) = @_; + $self->new(%$self, @but); +} + 1; diff --git a/t/basic.t b/t/basic.t index 2175524..c65b4ad 100644 --- a/t/basic.t +++ b/t/basic.t @@ -116,7 +116,7 @@ my $inner_op = make_op(FromCode->new( my ($self, $state) = @_; my @stack = @{$state->return_stack}; my $top = pop @stack; - $state->new(%$state, return_stack => \@stack, next_op => $top); + $state->but(return_stack => \@stack, next_op => $top); } )); @@ -127,10 +127,10 @@ my $call_op = FromCode->new( my $save_scope = $state->scope; my %scope = (S => $save_scope->{S}); my $ret_op = FromCode->new( - code => sub { $_[1]->new(%{$_[1]}, scope => $save_scope, next_op => $_[0]->next) }, + code => sub { $_[1]->but(scope => $save_scope, next_op => $_[0]->next) }, next => $self->next, ); - $state->new(%$state, + $state->but( scope => \%scope, return_stack => [ @rst, $ret_op ], next_op => $inner_op