From: Matt S Trout Date: Mon, 3 Feb 2014 02:22:51 +0000 (+0000) Subject: factor variable expansion out of assignment X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=606537d11c86a39c770d5fc681f402b5ff2c741c;p=scpubgit%2FDKit.git factor variable expansion out of assignment --- diff --git a/lib/DX/State.pm b/lib/DX/State.pm index 6f8a1f5..c430176 100644 --- a/lib/DX/State.pm +++ b/lib/DX/State.pm @@ -27,24 +27,33 @@ sub scope_var { } sub allocate_var { - my ($self, $name, $var) = @_; - my $id = join('_', $name, ++($self->id_gen->{$name})); + my ($self, $name, $var, $id_gen) = @_; + my $id = join('_', $name, ++($id_gen->{$name}||='000')); DX::Var->new(id => $id, %$var); } -sub assign_vars { +sub expand_vars { my ($self, %vars) = @_; my %by_id = %{$self->by_id}; - my $state = $self->but(id_gen => { %{$self->id_gen} }); + my %id_gen = %{$self->id_gen}; foreach my $name (keys %vars) { unless (blessed($vars{$name})) { - my $var = $vars{$name} = $state->allocate_var($name, $vars{$name}); + my $var = $vars{$name} = $self->allocate_var( + $name, $vars{$name}, \%id_gen + ); $by_id{$var->id} = $var; } } + $self->but( + by_id => \%by_id, id_gen => \%id_gen, + ), %vars; +} + +sub assign_vars { + my ($self, %vars) = @_; + my ($state, %expanded) = $self->expand_vars(%vars); $state->but( - by_id => \%by_id, - scope => { %{$self->scope}, map +($_ => $vars{$_}->id), keys %vars } + scope => { %{$self->scope}, map +($_ => $expanded{$_}->id), keys %expanded } ); }