<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>Thank you again for your useful hints during our meetings, I really learned a lot of Perl !<br><br>It could be pretty reduced, but I had a problem with line terminators ... some ugly ^M appeared everywhere, I think it is a Windows related issue. Even with the { binmode => 1 } option I got ^M when calling templates from other templates with INCLUDE, PROCESS or WRAPPER directives.<br><br>Probably this shorter code works pretty good on Linux, using paths instead of references ...<br></div><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><br>use strict;<br>use warnings;<br>use File::Find;<br>use File::Spec;<br>use Template;<br><br>my $template_config = {<br>    STRICT => 1,<br>    DEBUG  => 1,<br>   
 TRIM   => 1<br>};<br><br>my $template = Template->new($template_config);<br><br>find(<br>    {<br>        wanted => sub {<br>            return unless /\.tt2$/;<br><br>            # naming convention: /path/to/my/file.ext.tt2<br>            # is template file for /path/to/my/file.ext<br>            my $template_path = $File::Find::name;<br>            my $output_path   = $template_path;<br>            $output_path =~ s/\.tt2$//;<br><br>            # process
 templates<br>            $template->process( $template_path, {}, $output_path,<br>                { binmode => 1 } )<br>              or warn $template->error<br><br>              # but if there is an error don't commit changes<br>              and next;<br>          }<br>    },<br>    File::Spec->curdir<br>);<br><br>... but if you then want to tidy code, using references it's worth it.<br><br>Well, there are more than 100 ways to do it (and latins do it better :-) .<br><br><div style="font-family:arial, helvetica, sans-serif;font-size:10pt"><font
 face="Tahoma" size="2"><hr size="1"><b><span style="font-weight: bold;">Da:</span></b> marcos rebelo <oleber@gmail.com><br><b><span style="font-weight: bold;">A:</span></b> milan-pm@pm.org<br><b><span style="font-weight: bold;">Inviato:</span></b> Ven 25 marzo 2011, 15:22:31<br><b><span style="font-weight: bold;">Oggetto:</span></b> Re: [Milan-pm] Source code generation with Template Toolkit<br></font><br>Thanks for the consideration.<br><br>Lets reduce code ;)<br><br>use strict;<br>use warnings;<br>use File::Find;<br>use File::Spec;<br>use Template;<br>use Perl::Tidy;<br>use Slurp;<br><br>my $template_config = {<br>    STRICT => 1,<br>    DEBUG => 1,<br>    TRIM => 1<br>};<br><br>my $template = Template->new($template_config);<br><br>find({<br>    wanted => sub {<br>        if ( my ($output_path, $template_path) = ($File::Find::name =~<br>/((.)*\.tt2)$/ ) ) {<br> 
           print $template_path , "\n";<br><br>            my $template_content = read_file($template_path);<br><br>            # naming convention: /path/to/my/file.ext.tt2<br>            # is template file for /path/to/my/file.ext<br>            # if there is no /path/to/my/file.ext, just process<br>/path/to/my/file.ext.tt2<br><br>            if ( -e $output_path ) {<br><br>                print $output_path, "\n";<br>                # process templates<br>                $template->process(<br>                    \$template_content, {}, \my $output_content,<br>             
       { binmode => 1 }<br>                  )<br>                  or warn $template->error<br>                  # but if there is an error don't commit changes<br>                  and next;<br><br>                # clean ^M chars ... i'm on Windows<br>                my $m_char = chr(13);<br><br>                $output_content =~ s/$m_char//g;<br><br>                # if it is a module or a test, tidy it<br>                if ( $output_path =~ /\.(pm|t)$/ ) {<br>                    perltidy(<br>   
                     source => \$output_content,<br>                        destination => \my $output_content_tidy,<br>                        argv => []<br>                    );<br><br>                    # replace content with its tidy version<br>                    $output_content = $output_content_tidy;<br>                }<br><br>                write_file($output_path, $output_content);<br>            }<br>        }<br>    }<br>    File::Spec->curdir<br>);<br><br>Best
 Regards<br>MArcos<br><br>2011/3/25 Gianluca Casati <<a ymailto="mailto:casati_gianluca@yahoo.it" href="mailto:casati_gianluca@yahoo.it">casati_gianluca@yahoo.it</a>>:<br>> Hi all,<br>><br>> I just wrote an article about<br>><br>> Source code generation with Template Toolkit<br>><br>> You can read it here:<br>> <a href="http://perl-node-interface.blogspot.com/2011/03/source-code-generation-with-template.html" target="_blank">http://perl-node-interface.blogspot.com/2011/03/source-code-generation-with-template.html</a><br>><br>> Thanks to Marcos to share with me its solution !<br>><br>><br>> _______________________________________________<br>> Milan-pm mailing list<br>> <a ymailto="mailto:Milan-pm@pm.org" href="mailto:Milan-pm@pm.org">Milan-pm@pm.org</a><br>> <a href="http://mail.pm.org/mailman/listinfo/milan-pm"
 target="_blank">http://mail.pm.org/mailman/listinfo/milan-pm</a><br>><br>><br><br><br><br>-- <br>Marcos Rebelo<br><a href="http://www.oleber.com/" target="_blank">http://www.oleber.com/</a><br>Milan Perl Mongers leader <a href="https://sites.google.com/site/milanperlmongers/" target="_blank">https://sites.google.com/site/milanperlmongers/</a><br>Webmaster of <a href="http://perl5notebook.oleber.com" target="_blank">http://perl5notebook.oleber.com</a><br>_______________________________________________<br>Milan-pm mailing list<br><a ymailto="mailto:Milan-pm@pm.org" href="mailto:Milan-pm@pm.org">Milan-pm@pm.org</a><br><a href="http://mail.pm.org/mailman/listinfo/milan-pm" target="_blank">http://mail.pm.org/mailman/listinfo/milan-pm</a><br></div></div>



</div><br>



      </body></html>