$limit2 = Compute("a$xval+$c2")->reduce();what happens first is that Perl replaces
$xval
into the string "a$xval+$c"
to produce the string "a-5+9"
. That gets done before Compute()
is called, and so the expression that is used to create the MathObject is "a-5+9"
which Compute()
correctly interprets as a+4. It has no idea that the -5 comes from a perl variable originally.
When you use
$limit2 = Compute("a$xval+$c2")->reduce();however, the string is
"a*-5+9"
, which is -5a+9. Alternatively,
$limit2 = Compute("a($xval)+$c2")->reduce();produces
"a(-5)+9"
which again is -5a+9, and similarly for the other forms.
Had you used a MathObject Real object rather than a Perl real, then it would have inserted the parentheses automatically itself, and it would have worked as you expected, but Perl reals don't. That is, if you had used
$c2 = Real(non_zero_random(-9,9,1)); $xval = Real(non_zero_random(-9,9,1)); $limit2 = Compute("a$xval+$c2")->reduce();you would have gotten
"a(-5)+9"
, which would have done what you wanted.
Hope that clears things up.
Davide