Log Message:
-----------
Allow specification of values for multiple variables a once for
eval(), substitute() and so on. You can use
$f->eval(['x','y'] => Point(1,2));
or
$f->eval(['x','y'] => [1,2]);
or
$P = Point(1,2);
$f->eval(['x','y'] => $P);
to do the equivalent of
$f->eval(x=>1,y=>2);
Note that you must use quotes for the variable names and that they
must be enclosed in square brackets, not parentheses, when you supply
more than one variable. You can combine single with multiple
variables, as in
$f->eval(['x','y']=>Point(1,2),z=>3);
Modified Files:
--------------
pg/lib:
Parser.pm
Revision Data
-------------
Index: Parser.pm
===================================================================
RCS file: /webwork/cvs/system/pg/lib/Parser.pm,v
retrieving revision 1.46
retrieving revision 1.47
diff -Llib/Parser.pm -Llib/Parser.pm -u -r1.46 -r1.47
--- lib/Parser.pm
+++ lib/Parser.pm
@@ -730,18 +730,28 @@
# Sets the values of variables for evaluation purposes
#
sub setValues {
- my $self = shift; my ($value,$type); my $context = $self->context;
+ my $self = shift; my ($xref,$value,$type);
+ my $context = $self->context;
my $variables = $context->{variables};
- $self->{values} = {@_};
- foreach my $x (keys %{$self->{values}}) {
- $self->Error(["Undeclared variable '%s'",$x]) unless defined $variables->{$x};
- $value = Value::makeValue($self->{values}{$x},context=>$context);
- $value = $self->Package("Formula")->new($context,$value) unless Value::isValue($value);
- ($value,$type) = Value::getValueType($self,$value);
- $self->Error(["Variable '%s' should be of type %s",$x,$variables->{$x}{type}{name}])
- unless Parser::Item::typeMatch($type,$variables->{$x}{type});
- $value->inContext($self->context) if $value->context != $self->context;
- $self->{values}{$x} = $value;
+ while (scalar(@_)) {
+ $xref = shift; $value = shift;
+ if (ref($xref) eq "ARRAY") {
+ $value = [@{$value->{data}}] if Value::isValue($value);
+ $value = [$value] unless ref($value) eq 'ARRAY';
+ } else {
+ $xref = [$xref]; $value = [$value];
+ }
+ foreach my $i (0..scalar(@$xref)-1) {
+ my $x = $xref->[$i]; my $v = $value->[$i];
+ $self->Error(["Null value can't be assigned to variable '%s'",$x]) unless defined $v;
+ $self->Error(["Undeclared variable '%s'",$x]) unless defined $variables->{$x};
+ $v = Value::makeValue($v,context=>$context);
+ ($v,$type) = Value::getValueType($self,$v);
+ $self->Error(["Variable '%s' should be of type %s",$x,$variables->{$x}{type}{name}])
+ unless Parser::Item::typeMatch($type,$variables->{$x}{type});
+ $v->inContext($self->context) if $v->context != $self->context;
+ $self->{values}{$x} = $v;
+ }
}
}
|