Template Toolkitのprocessメソッドの返り値

メモ。Template Toolkitを使って、メール本文の雛形を作るにはどうすればよいか?私がわからんかったのは、processの返り値が標準出力らしいので雛形を処理させた結果をどうやって使えば良いかという点。

By default, the processed template output is printed to STDOUT. The process() method then returns 1 to indicate success. A third parameter may be passed to the process() method to specify a different output location. This value may be one of: a plain string indicating a filename which will be opened (relative to OUTPUT_PATH, if defined) and the output written to; a file GLOB opened ready for output; a reference to a scalar (e.g. a text string) to which output/error is appended; a reference to a subroutine which is called, passing the output as a parameter; or any object reference which implements a 'print' method (e.g. IO::Handle, Apache::Request, etc.) which will be called, passing the generated output as a parameter.

標準だとprocessは標準出力(STDOUT)に返り値を買えす。3番目の引数を指定すると別の場所に処理した結果を返す。

    # output filename
    $tt->process('welcome.tt2', $vars, 'welcome.html')
        || die $tt->error(), "\n";

    # reference to output subroutine
    sub myout {
        my $output = shift;
            ...
    }
    $tt->process('welcome.tt2', $vars, \&myout)
        || die $tt->error(), "\n";

    # reference to output text string
    my $output = '';
    $tt->process('welcome.tt2', $vars, \$output)
        || die $tt->error(), "\n";
    
    print "output: $output\n";

以上より、メール本文などの雛形を作ったときには

    # reference to output text string
    my $output = '';
    $tt->process('welcome.tt2', $vars, \$output)
        || die $tt->error(), "\n";
    
    print "output: $output\n";

を使えば良いことがわかる。