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

Currently I'm writing the output to a temporary file, then immediately reading the file into an array. Can I bypass the .tmp file altogether and output to a predefined array? (I need to store the whole html page for use later on).

Cheers

Replies are listed 'Best First'.
Re: HTML::Template output to scalar or array
by GrandFather (Saint) on Oct 11, 2005 at 20:47 UTC

    Simply use $myHTML = $template->output();


    Perl is Huffman encoded by design.
      I tried this:
      my $template = HTML::Template->new(filename => $prepage, die_on_bad_par +ams => 0); $template->param(Option_3 => $Option_3); $template->param(Option_4 => $Option_4); $template->param(Option_5 => $Option_5); $template->output(); print "$template"; exit;
      But it results in a blank screen. what have I misunderstood??

      Thanks

      Update: Doh! got it!

      my $html = $template->output(); print "$html"; exit;
      Thanks all for your help...
        Or just:

        print $template->output();

        if you're not doing anything with $html other than printing it. But yup, you've got it!

        Cheers,

        Brent

        -- Yeah, I'm a Delt.

        $template is an object. Unless it has an explicit stringification overload printing it is unlikely to do what you might hope for. Try my $str = $template->output (); print $str; instead.


        Perl is Huffman encoded by design.
Re: HTML::Template output to scalar or array
by saskaqueer (Friar) on Oct 11, 2005 at 20:47 UTC

    I'm assuming you have a block somewhat like the following?

    # ouch open(my $fh, '>', 'data.tmp') or die("open failed: $!"); $template->output(print_to => $fh); close($fh); open(my $fh, '<', 'data.tmp') or die("open failed: $!"); chomp( my @template_txt = <$fh> ); close($fh);

    Or maybe you're doing more harm than that even?

    # major ouch open(my $fh, '>', 'data.tmp') or die("open failed: $!"); print $fh $template->output(); close($fh); open(my $fh, '<', 'data.tmp') or die("open failed: $!"); chomp( my @template_txt = <$fh> ); close($fh);

    Either way, the output() method without the 'print_to' parameter will return the template text as a scalar. If you want each line of the template text to be its own array entry (or all in one scalar), you'd want this:

    # one line per array entry, no ouch my @template_txt = split( /\n/, $template->output() ); # all in one scalar, no ouch my $template_txt = $template->output();

    update: minor updates to the original 'ouch' possibilities, in order to include the reading of file to array.

Re: HTML::Template output to scalar or array
by jasonk (Parson) on Oct 11, 2005 at 20:43 UTC

    Some context on how you are writing to the file would help, but without that all I can really say is that $template->output() should return the filled template.


    We're not surrounded, we're in a target-rich environment!