<?xml version="1.0" encoding="windows-1252"?>
<node id="974233" title="Configuration Files and Template::Toolkit2" created="2012-06-04 01:37:49" updated="2012-06-04 01:37:49">
<type id="120">
perlmeditation</type>
<author id="253915">
Skeeve</author>
<data>
<field name="doctext">
&lt;p&gt;
I don't know whether I had a brand new idea or whether this has already been done.
&lt;/p&gt;
&lt;p&gt;
I have the requirement to generate several report mails. For this I collect some data from some input files and generate mails to certain persons based on this data. The content of this mail is generated by Template::Toolkit2 data.
&lt;/p&gt;
&lt;p&gt;
So what I required is some configuration information for:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What are the input files&lt;/li&gt;
&lt;li&gt;What are the template files&lt;/li&gt;
&lt;li&gt;Which mail addresses do I have (needed to match names to mail addresses)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
My first intention was to use one of the Config::XXX modules. But when I came to the mail templates, I remembered something about T::T2: First level variables are copied and can't be changed, but the structures they point to can be changed. So my T::T2 mail templates look something like this:
&lt;/p&gt; 
&lt;code&gt;
[%-
Mail.from=    'my.sender@ddress';
Mail.subject= 'This is your requested report';
Mail.cc= [    'someone.in@cc',
              'someoneelse@cc',
         ];
%]
here the body comes
&lt;/code&gt;
&lt;p&gt;
So when invoking the $tt-&gt;process, I supply in my variables the "Mail" variable, pointing to an empty hash, and I will receive all the values set in the template:
&lt;code&gt;
    my $vars= {
        Mail   =&gt; {},
        # additionally my other variables
    };
    my $output= '';
    $tt-&gt;process( $template, $vars, \$output) or die $tt-&gt;error();
    # Here I now have:
    # $vars-&gt;{'Mail'}-&gt;{'subject'} etc.
&lt;/code&gt;
&lt;p&gt;
Now I use this approach additionally for my configuration file:
&lt;/p&gt;
&lt;code&gt;
[%-
# (String) Name of the main report file
Files.defects= 'defect.htm';

# (Hash) additional data files and what to do with asignee
# Structure &lt;Name of the data file&gt; =&gt; { 'recipient' =&gt; ('to'|'no breach to'|'cc') } 
Files.datafiles ={
	'File1.csv'   =&gt; { recipient =&gt; 'to'          },
	'another.csv' =&gt; { recipient =&gt; 'no breach to'},
	'More.csv'    =&gt; { recipient =&gt; 'cc'          },
};

# (Array) List of all mailtemplate file names
Files.templates=[
    'report1.tt',
    'report2.tt',
];

# (Hash) List of all known e-mail adresses
# Structure: &lt;Name as in datafile&gt; =&gt; &lt;e-mail @dress&gt;
Users.list={
    'Horst Porst'    =&gt; 'Horst.Porst@my.do.main',
    'Frank Schrank'  =&gt; 'Frank.Schrank@my.do.main',
    'Hans Dampf'     =&gt; 'Hans.Dampf@my.do.main',
    'Ijon Tichy'     =&gt; 'Ijon.Tichy@my.do.main',
};
%]
&lt;/code&gt;
&lt;p&gt;
The only things I do is then to invoke that template:
&lt;/p&gt;
&lt;code&gt;
my $config_file= 'sendreport.conf';
my $config= {
    Users =&gt; {},
    Files =&gt; {},
};
$tt-&gt;process($config_file, $config) or die $tt-&gt;error();
&lt;/code&gt;
&lt;p&gt;
After that I have all my data in the %$config hash.
&lt;/p&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-253915"&gt;
&lt;hr&gt;&lt;code&gt;s$$([},&amp;%#}/&amp;/]+}%&amp;{})*;#$&amp;&amp;s&amp;&amp;$^X.($'^"%]=\&amp;(|?*{%&lt;/code&gt;
&lt;br&gt;&lt;font color="red"&gt;&lt;code&gt;+&lt;/code&gt;&lt;/font&gt;&lt;code&gt;.+=%;.#_}\&amp;"^"-+%*).}%:##%}={~=~:.")&amp;e&amp;&amp;s""`$''`"e&lt;/code&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
