jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I'm playing around with HTML::Template and I'd like to output to a file rather than the screen.

I've tried:

#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; use HTML::Template; my $h="path/test.html"; # open the HTML template my $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters in the template $template->param(home => $ENV{HTTP_HOST}); $template->param(path => $ENV{PATH}); # send the obligatory Content-Type print "Content-Type: text/html\n\n"; open(FILE,">$h") || die "Failed opening file: $!"; print FILE "$template->output"; close FILE; print "okay";
which saves the following to the file:
HTML::Template=HASH(0x814a750)->output
Could anyone enlighten me, please?

Replies are listed 'Best First'.
Re: HTML::Template output to file
by sauoq (Abbot) on Dec 01, 2002 at 02:27 UTC

    Double quoted strings cause variables to be interpolated; they don't cause code to be executed.¹ $template->output is a method call. The variable, $template is interpolated into the string. Stringified, it looks like "HTML::Template=HASH(0x814a750)" which indicates that it is a hash reference which has been blessed as an HTML::Template. In other words, it's an HTML::Template object. The "->output" portion of that string is printed literally.

    So, to fix the problem, remove the double quotes (as LTJake suggested.)

    1. Yes, there are ways to get double quoted strings to execute code but that takes some trickery.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Thanks for the explanation, sauoq - it's nice to know why!!
Re: HTML::Template output to file
by LTjake (Prior) on Dec 01, 2002 at 01:50 UTC
    I did a quick test, and all i had to do to fix it was remove the quotes around $template->output. so it becomes:
    print FILE $template->output;
    HTH

    --
    Rock is dead. Long live paper and scissors!
      Much obliged, LTjake - that worked for me, too!
Re: HTML::Template output to file
by pg (Canon) on Dec 01, 2002 at 06:42 UTC
    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.
      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