I'm using tt2 for preparing tests in Physics and Mathematics.
I want to generate several variations of a base questions, so
I generate several random values of a parameter, say
[% INCLUDE question FOREACH x= [1..20 ] %]
Now, I need to compute the right and wrong aswers for questions
parametrically from x, and I often need sqrt, sin, log, etc.
I discovered that tt2 is not consistent when dealing with numerics,
for instance it is forbidden to use a leading minus:
[% x = - y %
division has to be surrounded by spaces
[% x = y/z %] generates a parse error, but [% x = y / z %] is ok
and this is very confusing for teachers.
Worse of all I was not able to discover a simple way of performing
computations, say sqrt(x). I tried to define a macro sqrt using perl,
[% MACRO sqrt(x) PERL;
print sqrt($stash->get('x'));
END %]
but I cannot write [% x = sqrt(3*y) %], I'm forced to
write [% tmp = 3 * y; x = sqrt(tmp) %].
Then I tried to pass a tring to the macro, interpolate variables and then
eval it:
[% MACRO calc(x) BLOCK %]
[% PERL%]
my $txt = $stash->get('x');
$txt =~ s/\$(\w+)/\$stash->get\(\"$1\"\)/g;
my $x = eval($txt);
print $txt;
$txt = "";
[% END %]
[%END %]
but I discovered that already a simple
[% MACRO calc(x) BLOCK %]
[% PERL%]
my $txt = $stash->get('x');
$txt =~ s/(\w+)/$1/;
print $txt;
[% END %]
[%END %]
causes a core dump (linux, TT2 v. 2.05). why?
So my question: Is there a simple way of performing computations
like
[% v = sqrt(2*g*h) %] or not?
bye.
--
Franco Bagnoli (franchino) <[EMAIL PROTECTED]>
virtual location: Dipartimento di Energetica "S. Stecco"
real location: Dip. Matematica Applicata "G. Sansone", Universita' Firenze,
Via S. Marta, 3 I-50139 Firenze, Italy. Tel. +39 0554796422, fax: +39 055471787
GPG Key fingerprint = 169D 9EA5 8FD3 7EDA E43A 9830 255F BCEC 0D63 3728