| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363 | # -*- perl -*-# Text::Template.pm## Fill in `templates'## Copyright 2013 M. J. Dominus.# You may copy and distribute this program under the# same terms as Perl itself.# If in doubt, write to [email protected] for a license.#package Text::Template;$Text::Template::VERSION = '1.56';# ABSTRACT: Expand template text with embedded Perluse strict;use warnings;require 5.008;use base 'Exporter';our @EXPORT_OK = qw(fill_in_file fill_in_string TTerror);our $ERROR;my %GLOBAL_PREPEND = ('Text::Template' => '');sub Version {    $Text::Template::VERSION;}sub _param {    my ($k, %h) = @_;    for my $kk ($k, "\u$k", "\U$k", "-$k", "-\u$k", "-\U$k") {        return $h{$kk} if exists $h{$kk};    }    return undef;}sub always_prepend {    my $pack = shift;    my $old = $GLOBAL_PREPEND{$pack};    $GLOBAL_PREPEND{$pack} = shift;    $old;}{    my %LEGAL_TYPE;    BEGIN {        %LEGAL_TYPE = map { $_ => 1 } qw(FILE FILEHANDLE STRING ARRAY);    }    sub new {        my ($pack, %a) = @_;        my $stype     = uc(_param('type', %a) || "FILE");        my $source    = _param('source', %a);        my $untaint   = _param('untaint', %a);        my $prepend   = _param('prepend', %a);        my $alt_delim = _param('delimiters', %a);        my $broken    = _param('broken', %a);        my $encoding  = _param('encoding', %a);        unless (defined $source) {            require Carp;            Carp::croak("Usage: $ {pack}::new(TYPE => ..., SOURCE => ...)");        }        unless ($LEGAL_TYPE{$stype}) {            require Carp;            Carp::croak("Illegal value `$stype' for TYPE parameter");        }        my $self = {            TYPE     => $stype,            PREPEND  => $prepend,            UNTAINT  => $untaint,            BROKEN   => $broken,            ENCODING => $encoding,            (defined $alt_delim ? (DELIM => $alt_delim) : ())        };        # Under 5.005_03, if any of $stype, $prepend, $untaint, or $broken        # are tainted, all the others become tainted too as a result of        # sharing the expression with them.  We install $source separately        # to prevent it from acquiring a spurious taint.        $self->{SOURCE} = $source;        bless $self => $pack;        return unless $self->_acquire_data;        $self;    }}# Convert template objects of various types to type STRING,# in which the template data is embedded in the object itself.sub _acquire_data {    my $self = shift;    my $type = $self->{TYPE};    if ($type eq 'STRING') {        # nothing necessary    }    elsif ($type eq 'FILE') {        my $data = _load_text($self->{SOURCE});        unless (defined $data) {            # _load_text already set $ERROR            return undef;        }        if ($self->{UNTAINT} && _is_clean($self->{SOURCE})) {            _unconditionally_untaint($data);        }        if (defined $self->{ENCODING}) {            require Encode;            $data = Encode::decode($self->{ENCODING}, $data, &Encode::FB_CROAK);        }        $self->{TYPE}     = 'STRING';        $self->{FILENAME} = $self->{SOURCE};        $self->{SOURCE}   = $data;    }    elsif ($type eq 'ARRAY') {        $self->{TYPE} = 'STRING';        $self->{SOURCE} = join '', @{ $self->{SOURCE} };    }    elsif ($type eq 'FILEHANDLE') {        $self->{TYPE} = 'STRING';        local $/;        my $fh   = $self->{SOURCE};        my $data = <$fh>;             # Extra assignment avoids bug in Solaris perl5.00[45].        if ($self->{UNTAINT}) {            _unconditionally_untaint($data);        }        $self->{SOURCE} = $data;    }    else {        # This should have been caught long ago, so it represents a        # drastic `can't-happen' sort of failure        my $pack = ref $self;        die "Can only acquire data for $pack objects of subtype STRING, but this is $type; aborting";    }    $self->{DATA_ACQUIRED} = 1;}sub source {    my $self = shift;    $self->_acquire_data unless $self->{DATA_ACQUIRED};    return $self->{SOURCE};}sub set_source_data {    my ($self, $newdata, $type) = @_;    $self->{SOURCE}        = $newdata;    $self->{DATA_ACQUIRED} = 1;    $self->{TYPE}          = $type || 'STRING';    1;}sub compile {    my $self = shift;    return 1 if $self->{TYPE} eq 'PREPARSED';    return undef unless $self->_acquire_data;    unless ($self->{TYPE} eq 'STRING') {        my $pack = ref $self;        # This should have been caught long ago, so it represents a        # drastic `can't-happen' sort of failure        die "Can only compile $pack objects of subtype STRING, but this is $self->{TYPE}; aborting";    }    my @tokens;    my $delim_pats = shift() || $self->{DELIM};    my ($t_open, $t_close) = ('{', '}');    my $DELIM;    # Regex matches a delimiter if $delim_pats    if (defined $delim_pats) {        ($t_open, $t_close) = @$delim_pats;        $DELIM = "(?:(?:\Q$t_open\E)|(?:\Q$t_close\E))";        @tokens = split /($DELIM|\n)/, $self->{SOURCE};    }    else {        @tokens = split /(\\\\(?=\\*[{}])|\\[{}]|[{}\n])/, $self->{SOURCE};    }    my $state  = 'TEXT';    my $depth  = 0;    my $lineno = 1;    my @content;    my $cur_item = '';    my $prog_start;    while (@tokens) {        my $t = shift @tokens;        next if $t eq '';        if ($t eq $t_open) {    # Brace or other opening delimiter            if ($depth == 0) {                push @content, [ $state, $cur_item, $lineno ] if $cur_item ne '';                $cur_item   = '';                $state      = 'PROG';                $prog_start = $lineno;            }            else {                $cur_item .= $t;            }            $depth++;        }        elsif ($t eq $t_close) {    # Brace or other closing delimiter            $depth--;            if ($depth < 0) {                $ERROR = "Unmatched close brace at line $lineno";                return undef;            }            elsif ($depth == 0) {                push @content, [ $state, $cur_item, $prog_start ] if $cur_item ne '';                $state    = 'TEXT';                $cur_item = '';            }            else {                $cur_item .= $t;            }        }        elsif (!$delim_pats && $t eq '\\\\') {    # precedes \\\..\\\{ or \\\..\\\}            $cur_item .= '\\';        }        elsif (!$delim_pats && $t =~ /^\\([{}])$/) {    # Escaped (literal) brace?            $cur_item .= $1;        }        elsif ($t eq "\n") {                            # Newline            $lineno++;            $cur_item .= $t;        }        else {                                          # Anything else            $cur_item .= $t;        }    }    if ($state eq 'PROG') {        $ERROR = "End of data inside program text that began at line $prog_start";        return undef;    }    elsif ($state eq 'TEXT') {        push @content, [ $state, $cur_item, $lineno ] if $cur_item ne '';    }    else {        die "Can't happen error #1";    }    $self->{TYPE}   = 'PREPARSED';    $self->{SOURCE} = \@content;    1;}sub prepend_text {    my $self = shift;    my $t = $self->{PREPEND};    unless (defined $t) {        $t = $GLOBAL_PREPEND{ ref $self };        unless (defined $t) {            $t = $GLOBAL_PREPEND{'Text::Template'};        }    }    $self->{PREPEND} = $_[1] if $#_ >= 1;    return $t;}sub fill_in {    my ($fi_self, %fi_a) = @_;    unless ($fi_self->{TYPE} eq 'PREPARSED') {        my $delims = _param('delimiters', %fi_a);        my @delim_arg = (defined $delims ? ($delims) : ());        $fi_self->compile(@delim_arg)            or return undef;    }    my $fi_varhash    = _param('hash',       %fi_a);    my $fi_package    = _param('package',    %fi_a);    my $fi_broken     = _param('broken',     %fi_a) || $fi_self->{BROKEN} || \&_default_broken;    my $fi_broken_arg = _param('broken_arg', %fi_a) || [];    my $fi_safe       = _param('safe',       %fi_a);    my $fi_ofh        = _param('output',     %fi_a);    my $fi_filename   = _param('filename',   %fi_a) || $fi_self->{FILENAME} || 'template';    my $fi_strict     = _param('strict',     %fi_a);    my $fi_prepend    = _param('prepend',    %fi_a);    my $fi_eval_package;    my $fi_scrub_package = 0;    unless (defined $fi_prepend) {        $fi_prepend = $fi_self->prepend_text;    }    if (defined $fi_safe) {        $fi_eval_package = 'main';    }    elsif (defined $fi_package) {        $fi_eval_package = $fi_package;    }    elsif (defined $fi_varhash) {        $fi_eval_package  = _gensym();        $fi_scrub_package = 1;    }    else {        $fi_eval_package = caller;    }    my @fi_varlist;    my $fi_install_package;    if (defined $fi_varhash) {        if (defined $fi_package) {            $fi_install_package = $fi_package;        }        elsif (defined $fi_safe) {            $fi_install_package = $fi_safe->root;        }        else {            $fi_install_package = $fi_eval_package;    # The gensymmed one        }        @fi_varlist = _install_hash($fi_varhash => $fi_install_package);        if ($fi_strict) {            $fi_prepend = "use vars qw(@fi_varlist);$fi_prepend" if @fi_varlist;            $fi_prepend = "use strict;$fi_prepend";        }    }    if (defined $fi_package && defined $fi_safe) {        no strict 'refs';        # Big fat magic here: Fix it so that the user-specified package        # is the default one available in the safe compartment.        *{ $fi_safe->root . '::' } = \%{ $fi_package . '::' };    # LOD    }    my $fi_r = '';    my $fi_item;    foreach $fi_item (@{ $fi_self->{SOURCE} }) {        my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;        if ($fi_type eq 'TEXT') {            $fi_self->append_text_to_output(                text   => $fi_text,                handle => $fi_ofh,                out    => \$fi_r,                type   => $fi_type,);        }        elsif ($fi_type eq 'PROG') {            no strict;            my $fi_lcomment = "#line $fi_lineno $fi_filename";            my $fi_progtext = "package $fi_eval_package; $fi_prepend;\n$fi_lcomment\n$fi_text;\n;";            my $fi_res;            my $fi_eval_err = '';            if ($fi_safe) {                no strict;                no warnings;                $fi_safe->reval(q{undef $OUT});                $fi_res      = $fi_safe->reval($fi_progtext);                $fi_eval_err = $@;                my $OUT = $fi_safe->reval('$OUT');                $fi_res = $OUT if defined $OUT;            }            else {                no strict;                no warnings;                my $OUT;                $fi_res      = eval $fi_progtext;                $fi_eval_err = $@;                $fi_res      = $OUT if defined $OUT;            }            # If the value of the filled-in text really was undef,            # change it to an explicit empty string to avoid undefined            # value warnings later.            $fi_res = '' unless defined $fi_res;            if ($fi_eval_err) {                $fi_res = $fi_broken->(                    text   => $fi_text,                    error  => $fi_eval_err,                    lineno => $fi_lineno,                    arg    => $fi_broken_arg,);                if (defined $fi_res) {                    $fi_self->append_text_to_output(                        text   => $fi_res,                        handle => $fi_ofh,                        out    => \$fi_r,                        type   => $fi_type,);                }                else {                    return $fi_r;    # Undefined means abort processing                }            }            else {                $fi_self->append_text_to_output(                    text   => $fi_res,                    handle => $fi_ofh,                    out    => \$fi_r,                    type   => $fi_type,);            }        }        else {            die "Can't happen error #2";        }    }    _scrubpkg($fi_eval_package) if $fi_scrub_package;    defined $fi_ofh ? 1 : $fi_r;}sub append_text_to_output {    my ($self, %arg) = @_;    if (defined $arg{handle}) {        print { $arg{handle} } $arg{text};    }    else {        ${ $arg{out} } .= $arg{text};    }    return;}sub fill_this_in {    my ($pack, $text) = splice @_, 0, 2;    my $templ = $pack->new(TYPE => 'STRING', SOURCE => $text, @_)        or return undef;    $templ->compile or return undef;    my $result = $templ->fill_in(@_);    $result;}sub fill_in_string {    my $string = shift;    my $package = _param('package', @_);    push @_, 'package' => scalar(caller) unless defined $package;    Text::Template->fill_this_in($string, @_);}sub fill_in_file {    my $fn = shift;    my $templ = Text::Template->new(TYPE => 'FILE', SOURCE => $fn, @_) or return undef;    $templ->compile or return undef;    my $text = $templ->fill_in(@_);    $text;}sub _default_broken {    my %a = @_;    my $prog_text = $a{text};    my $err       = $a{error};    my $lineno    = $a{lineno};    chomp $err;    #  $err =~ s/\s+at .*//s;    "Program fragment delivered error ``$err''";}sub _load_text {    my $fn = shift;    open my $fh, '<', $fn or do {        $ERROR = "Couldn't open file $fn: $!";        return undef;    };    local $/;    <$fh>;}sub _is_clean {    my $z;    eval { ($z = join('', @_)), eval '#' . substr($z, 0, 0); 1 }    # LOD}sub _unconditionally_untaint {    for (@_) {        ($_) = /(.*)/s;    }}{    my $seqno = 0;    sub _gensym {        __PACKAGE__ . '::GEN' . $seqno++;    }    sub _scrubpkg {        my $s = shift;        $s =~ s/^Text::Template:://;        no strict 'refs';        my $hash = $Text::Template::{ $s . "::" };        foreach my $key (keys %$hash) {            undef $hash->{$key};        }        %$hash = ();        delete $Text::Template::{ $s . "::" };    }}# Given a hashful of variables (or a list of such hashes)# install the variables into the specified package,# overwriting whatever variables were there before.sub _install_hash {    my $hashlist = shift;    my $dest     = shift;    if (UNIVERSAL::isa($hashlist, 'HASH')) {        $hashlist = [$hashlist];    }    my @varlist;    for my $hash (@$hashlist) {        for my $name (keys %$hash) {            my $val = $hash->{$name};            no strict 'refs';            no warnings 'redefine';            local *SYM = *{"$ {dest}::$name"};            if (!defined $val) {                delete ${"$ {dest}::"}{$name};                my $match = qr/^.\Q$name\E$/;                @varlist = grep { $_ !~ $match } @varlist;            }            elsif (ref $val) {                *SYM = $val;                push @varlist, do {                    if    (UNIVERSAL::isa($val, 'ARRAY')) { '@' }                    elsif (UNIVERSAL::isa($val, 'HASH'))  { '%' }                    else                                  { '$' }                    }                    . $name;            }            else {                *SYM = \$val;                push @varlist, '$' . $name;            }        }    }    @varlist;}sub TTerror { $ERROR }1;__END__=pod=encoding UTF-8=head1 NAMEText::Template - Expand template text with embedded Perl=head1 VERSIONversion 1.56=head1 SYNOPSIS use Text::Template; $template = Text::Template->new(TYPE => 'FILE',  SOURCE => 'filename.tmpl'); $template = Text::Template->new(TYPE => 'ARRAY', SOURCE => [ ... ] ); $template = Text::Template->new(TYPE => 'FILEHANDLE', SOURCE => $fh ); $template = Text::Template->new(TYPE => 'STRING', SOURCE => '...' ); $template = Text::Template->new(PREPEND => q{use strict;}, ...); # Use a different template file syntax: $template = Text::Template->new(DELIMITERS => [$open, $close], ...); $recipient = 'King'; $text = $template->fill_in();  # Replaces `{$recipient}' with `King' print $text; $T::recipient = 'Josh'; $text = $template->fill_in(PACKAGE => T); # Pass many variables explicitly $hash = { recipient => 'Abed-Nego',           friends => [ 'me', 'you' ],           enemies => { loathsome => 'Saruman',                        fearsome => 'Sauron' },         }; $text = $template->fill_in(HASH => $hash, ...); # $recipient is Abed-Nego, # @friends is ( 'me', 'you' ), # %enemies is ( loathsome => ..., fearsome => ... ) # Call &callback in case of programming errors in template $text = $template->fill_in(BROKEN => \&callback, BROKEN_ARG => $ref, ...); # Evaluate program fragments in Safe compartment with restricted permissions $text = $template->fill_in(SAFE => $compartment, ...); # Print result text instead of returning it $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...); # Parse template with different template file syntax: $text = $template->fill_in(DELIMITERS => [$open, $close], ...); # Note that this is *faster* than using the default delimiters # Prepend specified perl code to each fragment before evaluating: $text = $template->fill_in(PREPEND => q{use strict 'vars';}, ...); use Text::Template 'fill_in_string'; $text = fill_in_string( <<EOM, PACKAGE => 'T', ...); Dear {$recipient}, Pay me at once.        Love,          G.V. EOM use Text::Template 'fill_in_file'; $text = fill_in_file($filename, ...); # All templates will always have `use strict vars' attached to all fragments Text::Template->always_prepend(q{use strict 'vars';});=head1 DESCRIPTIONThis is a library for generating form letters, building HTML pages, orfilling in templates generally.  A `template' is a piece of text thathas little Perl programs embedded in it here and there.  When you`fill in' a template, you evaluate the little programs and replacethem with their values.  You can store a template in a file outside your program.  People canmodify the template without modifying the program.  You can separatethe formatting details from the main code, and put the formattingparts of the program into the template.  That prevents code bloat andencourages functional separation.=head2 ExampleHere's an example of a template, which we'll suppose is stored in thefile C<formletter.tmpl>:    Dear {$title} {$lastname},    It has come to our attention that you are delinquent in your    {$monthname[$last_paid_month]} payment.  Please remit    ${sprintf("%.2f", $amount)} immediately, or your patellae may    be needlessly endangered.                    Love,                    Mark "Vizopteryx" DominusThe result of filling in this template is a string, which might looksomething like this:    Dear Mr. Smith,    It has come to our attention that you are delinquent in your    February payment.  Please remit    $392.12 immediately, or your patellae may    be needlessly endangered.                    Love,                    Mark "Vizopteryx" DominusHere is a complete program that transforms the exampletemplate into the example result, and prints it out:    use Text::Template;    my $template = Text::Template->new(SOURCE => 'formletter.tmpl')      or die "Couldn't construct template: $Text::Template::ERROR";    my @monthname = qw(January February March April May June                       July August September October November December);    my %vars = (title           => 'Mr.',                firstname       => 'John',                lastname        => 'Smith',                last_paid_month => 1,   # February                amount          => 392.12,                monthname       => \@monthname);    my $result = $template->fill_in(HASH => \%vars);    if (defined $result) { print $result }    else { die "Couldn't fill in template: $Text::Template::ERROR" }=head2 PhilosophyWhen people make a template module like this one, they almost alwaysstart by inventing a special syntax for substitutions.  For example,they build it so that a string like C<%%VAR%%> is replaced with thevalue of C<$VAR>.  Then they realize the need extra formatting, sothey put in some special syntax for formatting.  Then they need aloop, so they invent a loop syntax.  Pretty soon they have a newlittle template language.This approach has two problems: First, their little language iscrippled. If you need to do something the author hasn't thought of,you lose.  Second: Who wants to learn another language?  You alreadyknow Perl, so why not use it?C<Text::Template> templates are programmed in I<Perl>.  You embed Perlcode in your template, with C<{> at the beginning and C<}> at the end.If you want a variable interpolated, you write it the way you would inPerl.  If you need to make a loop, you can use any of the Perl loopconstructions.  All the Perl built-in functions are available.=head1 Details=head2 Template ParsingThe C<Text::Template> module scans the template source.  An open braceC<{> begins a program fragment, which continues until the matchingclose brace C<}>.  When the template is filled in, the programfragments are evaluated, and each one is replaced with the resultingvalue to yield the text that is returned.A backslash C<\> in front of a brace (or another backslash that is infront of a brace) escapes its special meaning.  The result of fillingout this template:    \{ The sum of 1 and 2 is {1+2}  \}is    { The sum of 1 and 2 is 3  }If you have an unmatched brace, C<Text::Template> will return afailure code and a warning about where the problem is.  Backslashesthat do not precede a brace are passed through unchanged.  If you havea template like this:    { "String that ends in a newline.\n" }The backslash inside the string is passed through to Perl unchanged,so the C<\n> really does turn into a newline.  See the note at the endfor details about the way backslashes work.  Backslash processing isI<not> done when you specify alternative delimiters with theC<DELIMITERS> option.  (See L<"Alternative Delimiters">, below.)Each program fragment should be a sequence of Perl statements, whichare evaluated the usual way.  The result of the last statementexecuted will be evaluated in scalar context; the result of thisstatement is a string, which is interpolated into the template inplace of the program fragment itself.The fragments are evaluated in order, and side effects from earlierfragments will persist into later fragments:    {$x = @things; ''}The Lord High Chamberlain has gotten {$x}    things for me this year.    { $diff = $x - 17;      $more = 'more'      if ($diff == 0) {        $diff = 'no';      } elsif ($diff < 0) {        $more = 'fewer';      }      '';    }    That is {$diff} {$more} than he gave me last year.The value of C<$x> set in the first line will persist into the nextfragment that begins on the third line, and the values of C<$diff> andC<$more> set in the second fragment will persist and be interpolatedinto the last line.  The output will look something like this:    The Lord High Chamberlain has gotten 42    things for me this year.    That is 25 more than he gave me last year.That is all the syntax there is.=head2 The C<$OUT> variableThere is one special trick you can play in a template.  Here is themotivation for it:  Suppose you are going to pass an array, C<@items>,into the template, and you want the template to generate a bulletedlist with a header, like this:    Here is a list of the things I have got for you since 1907:      * Ivory      * Apes      * Peacocks      * ...One way to do it is with a template like this:    Here is a list of the things I have got for you since 1907:    { my $blist = '';      foreach $i (@items) {          $blist .= qq{  * $i\n};      }      $blist;    }Here we construct the list in a variable called C<$blist>, which wereturn at the end.  This is a little cumbersome.  There is a shortcut.Inside of templates, there is a special variable called C<$OUT>.Anything you append to this variable will appear in the output of thetemplate.  Also, if you use C<$OUT> in a program fragment, the normalbehavior, of replacing the fragment with its return value, isdisabled; instead the fragment is replaced with the value of C<$OUT>.This means that you can write the template above like this:    Here is a list of the things I have got for you since 1907:    { foreach $i (@items) {        $OUT .= "  * $i\n";      }    }C<$OUT> is reinitialized to the empty string at the start of eachprogram fragment.  It is private to C<Text::Template>, so you can't use a variable named C<$OUT> in your template withoutinvoking the special behavior.=head2 General RemarksAll C<Text::Template> functions return C<undef> on failure, and set thevariable C<$Text::Template::ERROR> to contain an explanation of whatwent wrong.  For example, if you try to create a template from a filethat does not exist, C<$Text::Template::ERROR> will contain something like:    Couldn't open file xyz.tmpl: No such file or directory=head2 C<new>    $template = Text::Template->new( TYPE => ..., SOURCE => ... );This creates and returns a new template object.  C<new> returnsC<undef> and sets C<$Text::Template::ERROR> if it can't create thetemplate object.  C<SOURCE> says where the template source code willcome from.  C<TYPE> says what kind of object the source is.The most common type of source is a file:    Text::Template->new( TYPE => 'FILE', SOURCE => $filename );This reads the template from the specified file.  The filename isopened with the Perl C<open> command, so it can be a pipe or anythingelse that makes sense with C<open>.The C<TYPE> can also be C<STRING>, in which case the C<SOURCE> shouldbe a string:    Text::Template->new( TYPE => 'STRING',                         SOURCE => "This is the actual template!" );The C<TYPE> can be C<ARRAY>, in which case the source should be areference to an array of strings.  The concatenation of these stringsis the template:    Text::Template->new( TYPE => 'ARRAY',                             SOURCE => [ "This is ", "the actual",                                         " template!",                                       ]                       );The C<TYPE> can be FILEHANDLE, in which case the source should be anopen filehandle (such as you got from the C<FileHandle> or C<IO::*>packages, or a glob, or a reference to a glob).  In this caseC<Text::Template> will read the text from the filehandle up toend-of-file, and that text is the template:    # Read template source code from STDIN:    Text::Template->new ( TYPE => 'FILEHANDLE',                           SOURCE => \*STDIN  );If you omit the C<TYPE> attribute, it's taken to be C<FILE>.C<SOURCE> is required.  If you omit it, the program will abort.The words C<TYPE> and C<SOURCE> can be spelled any of the following ways:    TYPE     SOURCE    Type     Source    type     source    -TYPE    -SOURCE    -Type    -Source    -type    -sourcePick a style you like and stick with it.=over 4=item C<DELIMITERS>You may also add a C<DELIMITERS> option.  If this option is present,its value should be a reference to an array of two strings.  The firststring is the string that signals the beginning of each programfragment, and the second string is the string that signals the end ofeach program fragment.  See L<"Alternative Delimiters">, below.=item C<ENCODING>You may also add a C<ENCODING> option.  If this option is present, and theC<SOURCE> is a C<FILE>, then the data will be decoded from the given encodingusing the L<Encode> module.  You can use any encoding that L<Encode> recognizes.E.g.:    Text::Template->new(        TYPE     => 'FILE',        ENCODING => 'UTF-8',        SOURCE   => 'xyz.tmpl');=item C<UNTAINT>If your program is running in taint mode, you may have problems ifyour templates are stored in files.  Data read from files isconsidered 'untrustworthy', and taint mode will not allow you toevaluate the Perl code in the file.  (It is afraid that a maliciousperson might have tampered with the file.)In some environments, however, local files are trustworthy.  You cantell C<Text::Template> that a certain file is trustworthy by supplyingC<UNTAINT =E<gt> 1> in the call to C<new>.  This will tellC<Text::Template> to disable taint checks on template code that hascome from a file, as long as the filename itself is consideredtrustworthy.  It will also disable taint checks on template code thatcomes from a filehandle.  When used with C<TYPE =E<gt> 'string'> or C<TYPE=E<gt> 'array'>, it has no effect.See L<perlsec> for more complete information about tainting.Thanks to Steve Palincsar, Gerard Vreeswijk, and Dr. Christoph Baehrfor help with this feature.=item C<PREPEND>This option is passed along to the C<fill_in> call unless it isoverridden in the arguments to C<fill_in>.  See L<C<PREPEND> featureand using C<strict> in templates> below.=item C<BROKEN>This option is passed along to the C<fill_in> call unless it isoverridden in the arguments to C<fill_in>.  See L<C<BROKEN>> below.=back=head2 C<compile>    $template->compile()Loads all the template text from the template's source, parses andcompiles it.  If successful, returns true; otherwise returns false andsets C<$Text::Template::ERROR>.  If the template is already compiled,it returns true and does nothing.  You don't usually need to invoke this function, because C<fill_in>(see below) compiles the template if it isn't compiled already.If there is an argument to this function, it must be a reference to anarray containing alternative delimiter strings.  See C<"AlternativeDelimiters">, below.=head2 C<fill_in>    $template->fill_in(OPTIONS);Fills in a template.  Returns the resulting text if successful.Otherwise, returns C<undef>  and sets C<$Text::Template::ERROR>.The I<OPTIONS> are a hash, or a list of key-value pairs.  You canwrite the key names in any of the six usual styles as above; thismeans that where this manual says C<PACKAGE> (for example) you canactually use any of    PACKAGE Package package -PACKAGE -Package -packagePick a style you like and stick with it.  The all-lowercase versionsmay yield spurious warnings about    Ambiguous use of package => resolved to "package"so you might like to avoid them and use the capitalized versions.At present, there are eight legal options:  C<PACKAGE>, C<BROKEN>,C<BROKEN_ARG>, C<FILENAME>, C<SAFE>, C<HASH>, C<OUTPUT>, and C<DELIMITERS>.=over 4=item C<PACKAGE>C<PACKAGE> specifies the name of a package in which the programfragments should be evaluated.  The default is to use the package fromwhich C<fill_in> was called.  For example, consider this template:    The value of the variable x is {$x}.If you use C<$template-E<gt>fill_in(PACKAGE =E<gt> 'R')> , then the C<$x> inthe template is actually replaced with the value of C<$R::x>.  If youomit the C<PACKAGE> option, C<$x> will be replaced with the value ofthe C<$x> variable in the package that actually called C<fill_in>.You should almost always use C<PACKAGE>.  If you don't, and yourtemplate makes changes to variables, those changes will be propagatedback into the main program.  Evaluating the template in a privatepackage helps prevent this.  The template can still modify variablesin your program if it wants to, but it will have to do so explicitly.See the section at the end on `Security'.Here's an example of using C<PACKAGE>:    Your Royal Highness,    Enclosed please find a list of things I have gotten    for you since 1907:    { foreach $item (@items) {            $item_no++;        $OUT .= " $item_no. \u$item\n";      }    }    Signed,    Lord High ChamberlainWe want to pass in an array which will be assigned to the arrayC<@items>.  Here's how to do that:    @items = ('ivory', 'apes', 'peacocks', );    $template->fill_in();This is not very safe.  The reason this isn't as safe is that if youhad a variable named C<$item_no> in scope in your program at the pointyou called C<fill_in>, its value would be clobbered by the act offilling out the template.  The problem is the same as if you hadwritten a subroutine that used those variables in the same way thatthe template does.  (C<$OUT> is special in templates and is alwayssafe.)One solution to this is to make the C<$item_no> variable private to thetemplate by declaring it with C<my>.  If the template does this, youare safe.But if you use the C<PACKAGE> option, you will probably be safe evenif the template does I<not> declare its variables with C<my>:    @Q::items = ('ivory', 'apes', 'peacocks', );    $template->fill_in(PACKAGE => 'Q');In this case the template will clobber the variable C<$Q::item_no>,which is not related to the one your program was using.Templates cannot affect variables in the main program that aredeclared with C<my>, unless you give the template references to thosevariables.=item C<HASH>You may not want to put the template variables into a package.Packages can be hard to manage:  You can't copy them, for example.C<HASH> provides an alternative.  The value for C<HASH> should be a reference to a hash that mapsvariable names to values.  For example,     $template->fill_in(        HASH => {            recipient => "The King",            items     => ['gold', 'frankincense', 'myrrh'],            object    => \$self,        }    );will fill out the template and use C<"The King"> as the value ofC<$recipient> and the list of items as the value of C<@items>.  Notethat we pass an array reference, but inside the template it appears asan array.  In general, anything other than a simple string or numbershould be passed by reference.We also want to pass an object, which is in C<$self>; note that wepass a reference to the object, C<\$self> instead.  Since we've passeda reference to a scalar, inside the template the object appears asC<$object>.  The full details of how it works are a little involved, so you mightwant to skip to the next section.Suppose the key in the hash is I<key> and the value is I<value>.  =over 4=item *If the I<value> is C<undef>, then any variables named C<$key>,C<@key>, C<%key>, etc., are undefined.  =item *If the I<value> is a string or a number, then C<$key> is set to thatvalue in the template.=item *For anything else, you must pass a reference.If the I<value> is a reference to an array, then C<@key> is set tothat array.  If the I<value> is a reference to a hash, then C<%key> isset to that hash.  Similarly if I<value> is any other kind ofreference.  This means that    var => "foo"and    var => \"foo"have almost exactly the same effect.  (The difference is that in theformer case, the value is copied, and in the latter case it isaliased.)  =item *In particular, if you want the template to get an object or any kind,you must pass a reference to it:    $template->fill_in(HASH => { database_handle => \$dbh, ... });If you do this, the template will have a variable C<$database_handle>which is the database handle object.  If you leave out the C<\>, thetemplate will have a hash C<%database_handle>, which exposes theinternal structure of the database handle object; you don't want that.=backNormally, the way this works is by allocating a private package,loading all the variables into the package, and then filling out thetemplate as if you had specified that package.  A new package isallocated each time.  However, if you I<also> use the C<PACKAGE>option, C<Text::Template> loads the variables into the package youspecified, and they stay there after the call returns.  Subsequentcalls to C<fill_in> that use the same package will pick up the valuesyou loaded in.If the argument of C<HASH> is a reference to an array instead of areference to a hash, then the array should contain a list of hasheswhose contents are loaded into the template package one after theother.  You can use this feature if you want to combine several setsof variables.  For example, one set of variables might be the defaultsfor a fill-in form, and the second set might be the user inputs, whichoverride the defaults when they are present:    $template->fill_in(HASH => [\%defaults, \%user_input]);You can also use this to set two variables with the same name:    $template->fill_in(        HASH => [            { v => "The King" },            { v => [1,2,3] }        ]    );This sets C<$v> to C<"The King"> and C<@v> to C<(1,2,3)>.=item C<BROKEN>If any of the program fragments fails to compile or aborts for anyreason, and you have set the C<BROKEN> option to a function reference,C<Text::Template> will invoke the function.  This function is calledthe I<C<BROKEN> function>.  The C<BROKEN> function will tellC<Text::Template> what to do next.  If the C<BROKEN> function returns C<undef>, C<Text::Template> willimmediately abort processing the template and return the text that ithas accumulated so far.  If your function does this, it should set aflag that you can examine after C<fill_in> returns so that you cantell whether there was a premature return or not. If the C<BROKEN> function returns any other value, that value will beinterpolated into the template as if that value had been the returnvalue of the program fragment to begin with.  For example, if theC<BROKEN> function returns an error string, the error string will beinterpolated into the output of the template in place of the programfragment that cased the error.If you don't specify a C<BROKEN> function, C<Text::Template> suppliesa default one that returns something like    Program fragment delivered error ``Illegal division by 0 at    template line 37''(Note that the format of this message has changed slightly sinceversion 1.31.)  The return value of the C<BROKEN> function isinterpolated into the template at the place the error occurred, sothat this template:    (3+4)*5 = { 3+4)*5 }yields this result:    (3+4)*5 = Program fragment delivered error ``syntax error at template line 1''If you specify a value for the C<BROKEN> attribute, it should be areference to a function that C<fill_in> can call instead of thedefault function.C<fill_in> will pass a hash to the C<broken> function.The hash will have at least these three members:=over 4=item C<text>The source code of the program fragment that failed=item C<error>The text of the error message (C<$@>) generated by eval.The text has been modified to omit the trailing newline and to includethe name of the template file (if there was one).  The line numbercounts from the beginning of the template, not from the beginning ofthe failed program fragment.=item C<lineno>The line number of the template at which the program fragment began.=backThere may also be an C<arg> member.  See C<BROKEN_ARG>, below=item C<BROKEN_ARG>If you supply the C<BROKEN_ARG> option to C<fill_in>, the value of theoption is passed to the C<BROKEN> function whenever it is called.  Thedefault C<BROKEN> function ignores the C<BROKEN_ARG>, but you canwrite a custom C<BROKEN> function that uses the C<BROKEN_ARG> to getmore information about what went wrong. The C<BROKEN> function could also use the C<BROKEN_ARG> as a referenceto store an error message or some other information that it wants tocommunicate back to the caller.  For example:    $error = '';    sub my_broken {       my %args = @_;       my $err_ref = $args{arg};       ...       $$err_ref = "Some error message";       return undef;    }    $template->fill_in(        BROKEN     => \&my_broken,        BROKEN_ARG => \$error    );    if ($error) {      die "It didn't work: $error";    }If one of the program fragments in the template fails, it will callthe C<BROKEN> function, C<my_broken>, and pass it the C<BROKEN_ARG>,which is a reference to C<$error>.  C<my_broken> can store an errormessage into C<$error> this way.  Then the function that calledC<fill_in> can see if C<my_broken> has left an error message for itto find, and proceed accordingly.=item C<FILENAME>If you give C<fill_in> a C<FILENAME> option, then this is the file name thatyou loaded the template source from.  This only affects the error message thatis given for template errors.  If you loaded the template from C<foo.txt> forexample, and pass C<foo.txt> as the C<FILENAME> parameter, errors will looklike C<... at foo.txt line N> rather than C<... at template line N>. Note that this does NOT have anything to do with loading a template from thegiven filename.  See C<fill_in_file()> for that.For example: my $template = Text::Template->new(     TYPE   => 'string',     SOURCE => 'The value is {1/0}'); $template->fill_in(FILENAME => 'foo.txt') or die $Text::Template::ERROR;will die with an error that contains Illegal division by zero at at foo.txt line 1=item C<SAFE>If you give C<fill_in> a C<SAFE> option, its value should be a safecompartment object from the C<Safe> package.  All evaluation ofprogram fragments will be performed in this compartment.  See L<Safe>for full details about such compartments and how to restrict theoperations that can be performed in them.If you use the C<PACKAGE> option with C<SAFE>, the package you specifywill be placed into the safe compartment and evaluation will takeplace in that package as usual.  If not, C<SAFE> operation is a little different from the default.Usually, if you don't specify a package, evaluation of programfragments occurs in the package from which the template was invoked.But in C<SAFE> mode the evaluation occurs inside the safe compartmentand cannot affect the calling package.  Normally, if you use C<HASH>without C<PACKAGE>, the hash variables are imported into a private,one-use-only package.  But if you use C<HASH> and C<SAFE> togetherwithout C<PACKAGE>, the hash variables will just be loaded into theroot namespace of the C<Safe> compartment.=item C<OUTPUT>If your template is going to generate a lot of text that you are justgoing to print out again anyway,  you can save memory by havingC<Text::Template> print out the text as it is generated instead ofmaking it into a big string and returning the string.  If you supplythe C<OUTPUT> option to C<fill_in>, the value should be a filehandle.The generated text will be printed to this filehandle as it isconstructed.  For example:    $template->fill_in(OUTPUT => \*STDOUT, ...);fills in the C<$template> as usual, but the results are immediatelyprinted to STDOUT.  This may result in the output appearing morequickly than it would have otherwise.If you use C<OUTPUT>, the return value from C<fill_in> is still true onsuccess and false on failure, but the complete text is not returned tothe caller.=item C<PREPEND>You can have some Perl code prepended automatically to the beginningof every program fragment.  See L<C<PREPEND> feature and usingC<strict> in templates> below.=item C<DELIMITERS>If this option is present, its value should be a reference to a listof two strings.  The first string is the string that signals thebeginning of each program fragment, and the second string is thestring that signals the end of each program fragment.  SeeL<"Alternative Delimiters">, below.  If you specify C<DELIMITERS> in the call to C<fill_in>, they overrideany delimiters you set when you created the template object withC<new>. =back=head1 Convenience Functions=head2 C<fill_this_in>The basic way to fill in a template is to create a template object andthen call C<fill_in> on it.   This is useful if you want to fill inthe same template more than once.In some programs, this can be cumbersome.  C<fill_this_in> accepts astring, which contains the template, and a list of options, which arepassed to C<fill_in> as above.  It constructs the template object foryou, fills it in as specified, and returns the results.  It returnsC<undef> and sets C<$Text::Template::ERROR> if it couldn't generateany results.An example:    $Q::name = 'Donald';    $Q::amount = 141.61;    $Q::part = 'hyoid bone';    $text = Text::Template->fill_this_in( <<'EOM', PACKAGE => Q);    Dear {$name},    You owe me \\${sprintf('%.2f', $amount)}.    Pay or I will break your {$part}.        Love,        Grand Vizopteryx of Irkutsk.    EOMNotice how we included the template in-line in the program by using a`here document' with the C<E<lt>E<lt>> notation.C<fill_this_in> is a deprecated feature.  It is only here forbackwards compatibility, and may be removed in some far-future versionin C<Text::Template>.  You should use C<fill_in_string> instead.  Itis described in the next section.=head2 C<fill_in_string>It is stupid that C<fill_this_in> is a class method.  It should havebeen just an imported function, so that you could omit theC<Text::Template-E<gt>> in the example above.  But I made the mistakefour years ago and it is too late to change it.C<fill_in_string> is exactly like C<fill_this_in> except that it isnot a method and you can omit the C<Text::Template-E<gt>> and just say    print fill_in_string(<<'EOM', ...);    Dear {$name},      ...    EOMTo use C<fill_in_string>, you need to say    use Text::Template 'fill_in_string';at the top of your program.   You should probably useC<fill_in_string> instead of C<fill_this_in>.=head2 C<fill_in_file>If you import C<fill_in_file>, you can say    $text = fill_in_file(filename, ...);The C<...> are passed to C<fill_in> as above.  The filename is thename of the file that contains the template you want to fill in.  Itreturns the result text. or C<undef>, as usual.If you are going to fill in the same file more than once in the sameprogram you should use the longer C<new> / C<fill_in> sequence instead.It will be a lot faster because it only has to read and parse the fileonce.=head2 Including files into templatesPeople always ask for this.  ``Why don't you have an includefunction?'' they want to know.  The short answer is this is Perl, andPerl already has an include function.  If you want it, you can just put    {qx{cat filename}}into your template.  VoilE<agrave>.If you don't want to use C<cat>, you can write a little four-linefunction that opens a file and dumps out its contents, and call itfrom the template.  I wrote one for you.  In the template, you can say    {Text::Template::_load_text(filename)}If that is too verbose, here is a trick.  Suppose the template packagethat you are going to be mentioning in the C<fill_in> call is packageC<Q>.  Then in the main program, write    *Q::include = \&Text::Template::_load_text;This imports the C<_load_text> function into package C<Q> with thename C<include>.  From then on, any template that you fill in withpackage C<Q> can say    {include(filename)}to insert the text from the named file at that point.  If you areusing the C<HASH> option instead, just put C<include =E<gt>\&Text::Template::_load_text> into the hash instead of importing itexplicitly.Suppose you don't want to insert a plain text file, but rather youwant to include one template within another?  Just use C<fill_in_file>in the template itself:    {Text::Template::fill_in_file(filename)}You can do the same importing trick if this is too much to type.=head1 Miscellaneous=head2 C<my> variablesPeople are frequently surprised when this doesn't work:    my $recipient = 'The King';    my $text = fill_in_file('formletter.tmpl');The text C<The King> doesn't get into the form letter.  Why not?Because C<$recipient> is a C<my> variable, and the whole point ofC<my> variables is that they're private and inaccessible except in thescope in which they're declared.  The template is not part of thatscope, so the template can't see C<$recipient>.  If that's not the behavior you want, don't use C<my>.  C<my> means aprivate variable, and in this case you don't want the variable to beprivate.  Put the variables into package variables in some otherpackage, and use the C<PACKAGE> option to C<fill_in>:    $Q::recipient = $recipient;    my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q');or pass the names and values in a hash with the C<HASH> option:    my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient });=head2 Security MattersAll variables are evaluated in the package you specify with theC<PACKAGE> option of C<fill_in>.  if you use this option, and if yourtemplates don't do anything egregiously stupid, you won't have toworry that evaluation of the little programs will creep out into therest of your program and wreck something.Nevertheless, there's really no way (except with C<Safe>) to protectagainst a template that says    { $Important::Secret::Security::Enable = 0;      # Disable security checks in this program    }or    { $/ = "ho ho ho";   # Sabotage future uses of <FH>.      # $/ is always a global variable    }or even    { system("rm -rf /") }so B<don't> go filling in templates unless you're sure you know what'sin them.  If you're worried, or you can't trust the person who wrotethe template, use the C<SAFE> option.A final warning: program fragments run a small risk of accidentallyclobbering local variables in the C<fill_in> function itself.  Thesevariables all have names that begin with C<$fi_>, so if you stay awayfrom those names you'll be safe.  (Of course, if you're a real wizardyou can tamper with them deliberately for exciting effects; this isactually how C<$OUT> works.)  I can fix this, but it will make thepackage slower to do it, so I would prefer not to.  If you are worriedabout this, send me mail and I will show you what to do about it.=head2 Alternative DelimitersLorenzo Valdettaro pointed out that if you are using C<Text::Template>to generate TeX output, the choice of braces as the program fragmentdelimiters makes you suffer suffer suffer.  Starting in version 1.20,you can change the choice of delimiters to something other than curlybraces.In either the C<new()> call or the C<fill_in()> call, you can specifyan alternative set of delimiters with the C<DELIMITERS> option.  Forexample, if you would like code fragments to be delimited by C<[@-->and C<--@]> instead of C<{> and C<}>, use    ... DELIMITERS => [ '[@--', '--@]' ], ...Note that these delimiters are I<literal strings>, not regexes.  (Itried for regexes, but it complicates the lexical analysis too much.)Note also that C<DELIMITERS> disables the special meaning of thebackslash, so if you want to include the delimiters in the literaltext of your template file, you are out of luck---it is up to you tochoose delimiters that do not conflict with what you are doing.  Thedelimiter strings may still appear inside of program fragments as longas they nest properly.  This means that if for some reason youabsolutely must have a program fragment that mentions one of thedelimiters, like this:    [@--        print "Oh no, a delimiter: --@]\n"    --@]you may be able to make it work by doing this instead:    [@--        # Fake matching delimiter in a comment: [@--        print "Oh no, a delimiter: --@]\n"    --@]It may be safer to choose delimiters that begin with a newlinecharacter.  Because the parsing of templates is simplified by the absence ofbackslash escapes, using alternative C<DELIMITERS> may speed up theparsing process by 20-25%.  This shows that my original choice of C<{>and C<}> was very bad. =head2 C<PREPEND> feature and using C<strict> in templatesSuppose you would like to use C<strict> in your templates to detectundeclared variables and the like.  But each code fragment is aseparate lexical scope, so you have to turn on C<strict> at the top ofeach and every code fragment:    { use strict;      use vars '$foo';      $foo = 14;      ...    }    ...    { # we forgot to put `use strict' here      my $result = $boo + 12;    # $boo is misspelled and should be $foo      # No error is raised on `$boo'    }Because we didn't put C<use strict> at the top of the second fragment,it was only active in the first fragment, and we didn't get anyC<strict> checking in the second fragment.  Then we misspelled C<$foo>and the error wasn't caught.  C<Text::Template> version 1.22 and higher has a new feature to makethis easier.  You can specify that any text at all be automaticallyadded to the beginning of each program fragment.  When you make a call to C<fill_in>, you can specify a    PREPEND => 'some perl statements here'option; the statements will be prepended to each program fragment forthat one call only.  Suppose that the C<fill_in> call included a    PREPEND => 'use strict;'option, and that the template looked like this:    { use vars '$foo';      $foo = 14;      ...    }    ...    { my $result = $boo + 12;    # $boo is misspelled and should be $foo      ...    }The code in the second fragment would fail, because C<$boo> has notbeen declared.  C<use strict> was implied, even though you did notwrite it explicitly, because the C<PREPEND> option added it for youautomatically.There are three other ways to do this.  At the time you create thetemplate object with C<new>, you can also supply a C<PREPEND> option,in which case the statements will be prepended each time you fill inthat template.  If the C<fill_in> call has its own C<PREPEND> option,this overrides the one specified at the time you created thetemplate.  Finally, you can make the class method call    Text::Template->always_prepend('perl statements');If you do this, then call calls to C<fill_in> for I<any> template willattach the perl statements to the beginning of each program fragment,except where overridden by C<PREPEND> options to C<new> or C<fill_in>.An alternative to adding "use strict;" to the PREPEND option, you canpass STRICT => 1 to fill_in when also passing the HASH option.Suppose that the C<fill_in> call included both    HASH   => {$foo => ''} and    STRICT => 1options, and that the template looked like this:    {      $foo = 14;      ...    }    ...    { my $result = $boo + 12;    # $boo is misspelled and should be $foo      ...    }The code in the second fragment would fail, because C<$boo> has notbeen declared. C<use strict> was implied, even though you did notwrite it explicitly, because the C<STRICT> option added it for youautomatically. Any variable referenced in the template that is not in theC<HASH> option will be an error.=head2 Prepending in Derived ClassesThis section is technical, and you should skip it on the first fewreadings. Normally there are three places that prepended text could come from.It could come from the C<PREPEND> option in the C<fill_in> call, fromthe C<PREPEND> option in the C<new> call that created the templateobject, or from the argument of the C<always_prepend> call.C<Text::Template> looks for these three things in order and takes thefirst one that it finds.In a subclass of C<Text::Template>, this last possibility isambiguous.  Suppose C<S> is a subclass of C<Text::Template>.  Should     Text::Template->always_prepend(...);affect objects in class C<Derived>?  The answer is that you can have iteither way.  The C<always_prepend> value for C<Text::Template> is normally storedin  a hash variable named C<%GLOBAL_PREPEND> under the keyC<Text::Template>.  When C<Text::Template> looks to see what text toprepend, it first looks in the template object itself, and if not, itlooks in C<$GLOBAL_PREPEND{I<class>}> where I<class> is the class towhich the template object belongs.  If it doesn't find any value, itlooks in C<$GLOBAL_PREPEND{'Text::Template'}>.  This means thatobjects in class C<Derived> I<will> be affected by    Text::Template->always_prepend(...);I<unless> there is also a call to    Derived->always_prepend(...);So when you're designing your derived class, you can arrange to haveyour objects ignore C<Text::Template::always_prepend> calls by simplyputting C<Derived-E<gt>always_prepend('')> at the top of your module.Of course, there is also a final escape hatch: Templates support aC<prepend_text> that is used to look up the appropriate text to beprepended at C<fill_in> time.  Your derived class can override thismethod to get an arbitrary effect.=head2 JavaScriptJennifer D. St Clair asks:    > Most of my pages contain JavaScript and Stylesheets.    > How do I change the template identifier?Jennifer is worried about the braces in the JavaScript being taken asthe delimiters of the Perl program fragments.  Of course, disasterwill ensue when perl tries to evaluate these as if they were Perlprograms.  The best choice is to find some unambiguous delimiterstrings that you can use in your template instead of curly braces, andthen use the C<DELIMITERS> option.  However, if you can't do this forsome reason, there are  two easy workarounds:1. You can put C<\> in front of C<{>, C<}>, or C<\> to remove itsspecial meaning.  So, for example, instead of    if (br== "n3") {         // etc.    }you can put    if (br== "n3") \{        // etc.    \}and it'll come out of the template engine the way you want.But here is another method that is probably better.  To see how itworks, first consider what happens if you put this into a template:    { 'foo' }Since it's in braces, it gets evaluated, and obviously, this is goingto turn into    fooSo now here's the trick: In Perl, C<q{...}> is the same as C<'...'>.So if we wrote    {q{foo}}it would turn into     fooSo for your JavaScript, just write    {q{if (br== "n3") {       // etc.       }}    }and it'll come out as    if (br== "n3") {        // etc.    }which is what you want.head2 Shut Up!People sometimes try to put an initialization section at the top oftheir templates, like this:    { ...        $var = 17;    }Then they complain because there is a C<17> at the top of the outputthat they didn't want to have there.  Remember that a program fragment is replaced with its own returnvalue, and that in Perl the return value of a code block is the valueof the last expression that was evaluated, which in this case is 17.If it didn't do that, you wouldn't be able to write C<{$recipient}>and have the recipient filled in.To prevent the 17 from appearing in the output is very simple:    { ...        $var = 17;        '';    }Now the last expression evaluated yields the empty string, which isinvisible.  If you don't like the way this looks, use    { ...        $var = 17;        ($SILENTLY);    }instead.  Presumably, C<$SILENTLY> has no value, so nothing will beinterpolated.  This is what is known as a `trick'.=head2 CompatibilityEvery effort has been made to make this module compatible with olderversions.  The only known exceptions follow:The output format of the default C<BROKEN> subroutine has changedtwice, most recently between versions 1.31 and 1.40.Starting in version 1.10, the C<$OUT> variable is arrogated for aspecial meaning.  If you had templates before version 1.10 thathappened to use a variable named C<$OUT>, you will have to change themto use some other variable or all sorts of strangeness will result.Between versions 0.1b and 1.00 the behavior of the \ metacharacterchanged.  In 0.1b, \\ was special everywhere, and the templateprocessor always replaced it with a single backslash before passingthe code to Perl for evaluation.  The rule now is more complicated butprobably more convenient.  See the section on backslash processing,below, for a full discussion.=head2 Backslash ProcessingIn C<Text::Template> beta versions, the backslash was special wheneverit appeared before a brace or another backslash.  That meant thatwhile C<{"\n"}> did indeed generate a newline, C<{"\\"}> did notgenerate a backslash, because the code passed to Perl for evaluationwas C<"\"> which is a syntax error.  If you wanted a backslash, youwould have had to write C<{"\\\\"}>.In C<Text::Template> versions 1.00 through 1.10, there was a bug:Backslash was special everywhere.  In these versions, C<{"\n"}>generated the letter C<n>.The bug has been corrected in version 1.11, but I did not go back toexactly the old rule, because I did not like the idea of having towrite C<{"\\\\"}> to get one backslash.  The rule is now morecomplicated to remember, but probably easier to use.  The rule is now:Backslashes are always passed to Perl unchanged I<unless> they occuras part of a sequence like C<\\\\\\{> or C<\\\\\\}>.  In thesecontexts, they are special; C<\\> is replaced with C<\>, and C<\{> andC<\}> signal a literal brace. Examples:    \{ foo \}is I<not> evaluated, because the C<\> before the braces signals thatthey should be taken literally.  The result in the output looks like this:     { foo }This is a syntax error:    { "foo}" }because C<Text::Template> thinks that the code ends at the first C<}>,and then gets upset when it sees the second one.  To make this workcorrectly, use    { "foo\}" }This passes C<"foo}"> to Perl for evaluation.  Note there's no C<\> inthe evaluated code.  If you really want a C<\> in the evaluated code,use    { "foo\\\}" }This passes C<"foo\}"> to Perl for evaluation.Starting with C<Text::Template> version 1.20, backslash processing isdisabled if you use the C<DELIMITERS> option to specify alternativedelimiter strings.=head2 A short note about C<$Text::Template::ERROR>In the past some people have fretted about `violating the packageboundary' by examining a variable inside the C<Text::Template>package.  Don't feel this way.  C<$Text::Template::ERROR> is part ofthe published, official interface to this package.  It is perfectly OKto inspect this variable.  The interface is not going to change.If it really, really bothers you, you can import a function calledC<TTerror> that returns the current value of the C<$ERROR> variable.So you can say:    use Text::Template 'TTerror';    my $template = Text::Template->new(SOURCE => $filename);    unless ($template) {        my $err = TTerror;        die "Couldn't make template: $err; aborting";    }I don't see what benefit this has over just doing this:    use Text::Template;    my $template = Text::Template->new(SOURCE => $filename)        or die "Couldn't make template: $Text::Template::ERROR; aborting";But if it makes you happy to do it that way, go ahead.=head2 Sticky Widgets in Template FilesThe C<CGI> module provides functions for `sticky widgets', which areform input controls that retain their values from one page to thenext.   Sometimes people want to know how to include these widgetsinto their template output.It's totally straightforward.  Just call the C<CGI> functions frominside the template:    { $q->checkbox_group(NAME      => 'toppings',                         LINEBREAK => true,                         COLUMNS   => 3,                         VALUES    => \@toppings,                        );    }=head2 Automatic preprocessing of program fragmentsIt may be useful to preprocess the program fragments before they areevaluated.  See C<Text::Template::Preprocess> for more details.=head2 Automatic postprocessing of template hunksIt may be useful to process hunks of output before they are appended tothe result text.  For this, subclass and replace the C<append_text_to_result>method.  It is passed a list of pairs with these entries:  handle - a filehandle to which to print the desired output  out    - a ref to a string to which to append, to use if handle is not given  text   - the text that will be appended  type   - where the text came from: TEXT for literal text, PROG for code=head1 HISTORYOriginally written by Mark Jason Dominus, Plover Systems (versions 0.01 - 1.46)Maintainership transferred to Michael Schout E<lt>[email protected]<gt> in version1.47=head1 THANKSMany thanks to the following people for offering support,encouragement, advice, bug reports, and all the other good stuff.  =over 4=item *Andrew G Wood=item *Andy Wardley=item *António Aragão=item *Archie Warnock=item *Bek Oberin=item *Bob Dougherty=item *Brian C. Shensky=item *Chris Nandor=item *Chris Wesley=item *Chris.Brezil=item *Daini Xie=item *Dan Franklin=item *Daniel LaLiberte=item *David H. Adler=item *David Marshall=item *Dennis Taylor=item *Donald L. Greer Jr.=item *Dr. Frank Bucolo=item *Fred Steinberg=item *Gene Damon=item *Hans Persson=item *Hans Stoop=item *Itamar Almeida de Carvalho=item *James H. Thompson=item *James Mastros=item *Jarko Hietaniemi=item *Jason Moore=item *Jennifer D. St Clair=item *Joel Appelbaum=item *Joel Meulenberg=item *Jonathan Roy=item *Joseph Cheek=item *Juan E. Camacho=item *Kevin Atteson=item *Kevin Madsen=item *Klaus Arnhold=item *Larry Virden=item *Lieven Tomme=item *Lorenzo Valdettaro=item *Marek Grac=item *Matt Womer=item *Matt X. Hunter=item *Michael G Schwern=item *Michael J. Suzio=item *Michaely Yeung=item *Michelangelo Grigni=item *Mike Brodhead=item *Niklas Skoglund=item *Randal L. Schwartz=item *Reuven M. Lerner=item *Robert M. Ioffe=item *Ron Pero=item *San Deng=item *Sean Roehnelt=item *Sergey Myasnikov=item *Shabbir J. Safdar=item *Shad Todd=item *Steve Palincsar=item *Tim Bunce=item *Todd A. Green=item *Tom Brown=item *Tom Henry=item *Tom Snee=item *Trip Lilley=item *Uwe Schneider=item *Val Luck=item *Yannis Livassof=item *Yonat Sharon=item *Zac Hansen=item *gary at dls.net=backSpecial thanks to:=over 2=item Jonathan Roy for telling me how to do the C<Safe> support (I spent two yearsworrying about it, and then Jonathan pointed out that it was trivial.)=item Ranjit Bhatnagar for demanding less verbose fragments like they have in ASP, forhelping me figure out the Right Thing, and, especially, for talking meout of adding any new syntax.  These discussions resulted in theC<$OUT> feature.=back=head2 Bugs and CaveatsC<my> variables in C<fill_in> are still susceptible to being clobberedby template evaluation.  They all begin with C<fi_>, so avoid thosenames in your templates.The line number information will be wrong if the template's lines arenot terminated by C<"\n">.  You should let me know if this is aproblem.  If you do, I will fix it.The C<$OUT> variable has a special meaning in templates, so you cannotuse it as if it were a regular variable.There are not quite enough tests in the test suite.=head1 SOURCEThe development version is on github at L<https://https://github.com/mschout/perl-text-template>and may be cloned from L<git://https://github.com/mschout/perl-text-template.git>=head1 BUGSPlease report any bugs or feature requests on the bugtracker websiteL<https://github.com/mschout/perl-text-template/issues>When submitting a bug or request, please include a test-file or apatch to an existing test-file that illustrates the bug or desiredfeature.=head1 AUTHORMichael Schout <[email protected]>=head1 COPYRIGHT AND LICENSEThis software is copyright (c) 2013 by Mark Jason Dominus <[email protected]>.This is free software; you can redistribute it and/or modify it underthe same terms as the Perl 5 programming language system itself.=cut
 |