Howdy,

While playing around with HTML::Template, I found a nice behavior, which is problably documentented, but I haven't found it.
If you run this code

# the template is just <TMPL_VAR NAME="hallo"> use HTML::Template; use strict; my $t=new HTML::Template( filename => "/tmp/tmpl"); print "3 ".time()."\n"; $t->param( "hallo" => \&littletest ); sleep 3; print "2 ".time()."\n"; sleep 3; print $t->output; sub littletest { print "1 ".time()."\n"; return "test"; }
it prints out:
3 1019119182 2 1019119185 1 1019119188 test
But if you change the referenced sub to just the sub (delete the \), it prints out:
3 1019119294 1 1019119294 2 1019119297 test

Cool he

This allows you to setup parameters which will be evaluated at print time instead of assignment time.
If this isn't a new feature, well than I am just proud to have found a documented feature myself :-)
---------------------------
Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium

Replies are listed 'Best First'.
Re: Nice HTML::Template feature
by Juerd (Abbot) on Apr 18, 2002 at 10:30 UTC

    $t->param( "hallo" => \&littletest );

    That is more or less equal to:

    print "1 " . time() . "\n"; $t->param( "hallo" => "test" );
    Which explains why the "1" is printed before the "2".

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Hi,<pb> please note that 1 is printed before 2 in case of:
      $t->param( "hallo" => &littletest );
      And this is what one expects because it runs the sub before calling param.

      Or am I wrong??
      ---------------------------
      Dr. Mark Ceulemans
      Senior Consultant
      IT Masters, Belgium

        And this is what one expects because it runs the sub before calling param.

        Right. How can that be one of HTML::Template's feature? When you call a sub, the returned value is used, so it reads "hallo" => "test", but just before "test" is returned, the "1" with timestamp is printed.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.