On Tue, Feb 05, 2002 at 10:36:31AM +0100, Franco Bagnoli wrote:
> 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

[...]

> but I cannot write [% x = sqrt(3*y) %], I'm forced to
> write [% tmp = 3 * y; x = sqrt(tmp) %].

Yes, I'm afraid these are all limitations of the current parser, all of
which should be fixed in version 3, although that's not due for, erm, some
time yet.

>  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?

There is a deep, dark and insidious bug lurking within the MACRO code.
I forget the precise reason, but I spent some time hunting it down only
to realise that it's an unavoidable consequence of the way in which macros
are currently implemented.  Something to do with executing closures in a 
different scope to the one in which it was created that screws with Perl's
brains.  I'll try and remember/discover more about it and see if there's 
a fix, but I suspect it's something else that needs a new implementation 
for version 3.

> So my question: Is there a simple way of performing computations
> like
> 
> [% v = sqrt(2*g*h) %] or not?

As Mark has already pointed out, you can bind Perl subs to template 
variables to implement any kind of functionality you require.  Alas, 
you will need to use a temporary variable until the parser grows up 
a little:

  [% t = 2*g*h; v = sqrt(t) %]

The Math plugin would be the best idea, though.  It would be trivially 
easy to write.... in fact, here you go, I've attached a fledgling Math
plugin (and associated test script, of course :-).  I'd be more than
happy for you or anyone else to flesh this out with more functionality 
and I'll distribute it as a core plugin.

HTH

A


Attachment: Math.pm
Description: Perl program

Attachment: math.t
Description: Troff document

Reply via email to