in reply to HTML::Template output to file

One important point. The way you use output():
print FILE $template->output();
requires memory to hold the entire string returned from $template->output(), which can be quite big in some cases, and at the same time, slow down the process.

The output method does take a named parameter "print_to", which allows you to pass a file handler as the output file. If you specify this parameter, the method will write to that file on fly, and return undef, thus largely reduce the memory required.

Replies are listed 'Best First'.
Re: Re: HTML::Template output to file
by jonnyfolk (Vicar) on Dec 01, 2002 at 08:15 UTC
    I tried:
    print FILE $template->output(print_to);
    and
    print FILE $template->print_to;
    both to no avail.

    Any chance you could show me how that should be?

    Thanks.
      According to the docs, the desired syntax for your application is
      $template->output(print_to => \*FILE);

      In general, the syntax for named parameters is

      func(name => $value, ...); $obj->method(name => $value, ...);
      optionally with a hyphen in front of the name, which makes it look like a command line option (and which should ring the "familiarity" bell); and which makes it stand out a little more:
      func(-name => $value, ...); $obj->method(-name => $value, ...);
      In case you find this baffling: the autoquoting rule for the left hand side of "=>" also applies to barewords preceded with a hyphen. So, (-name => 'value') is interpreted by Perl as ('-name', 'value').

      But, from the source I would gather that HTML::Template doesn't honour this tradition, by which I mean it likely won't recognize $template->output(-print_to => \*FILE); . However, I musty admit I haven't tested it.

      From the manual to HTML::Template

      ***QUOTE***
      You may optionally supply a filehandle to print to automatically as the template is generated. This may improve performance and lower memory consumption. Example:

      $template->output(print_to => *STDOUT); The return value is undefined when using the ``print_to'' option.
      ***UNQUOTE***

      Karl

        I fear I was guilty in going to the example pages a little too quickly and not having gone back to check the module documentation.

        That having been said I am still at the stage where I find module documentation a little obscure (though I'm improving all the time), and the clarity of the responses I have received here have really helped me further to understand the documentation.

        I simply therefore thank all those who shared their wisdom for their patience, and assure them that their advice was useful to me to an extent far greater than simply 'reading the documentation'.

        Thank you.