[Milan-pm] Source code generation with Template Toolkit
marcos rebelo
oleber at gmail.com
Fri Mar 25 07:22:31 PDT 2011
Thanks for the consideration.
Lets reduce code ;)
use strict;
use warnings;
use File::Find;
use File::Spec;
use Template;
use Perl::Tidy;
use Slurp;
my $template_config = {
STRICT => 1,
DEBUG => 1,
TRIM => 1
};
my $template = Template->new($template_config);
find({
wanted => sub {
if ( my ($output_path, $template_path) = ($File::Find::name =~
/((.)*\.tt2)$/ ) ) {
print $template_path , "\n";
my $template_content = read_file($template_path);
# naming convention: /path/to/my/file.ext.tt2
# is template file for /path/to/my/file.ext
# if there is no /path/to/my/file.ext, just process
/path/to/my/file.ext.tt2
if ( -e $output_path ) {
print $output_path, "\n";
# process templates
$template->process(
\$template_content, {}, \my $output_content,
{ binmode => 1 }
)
or warn $template->error
# but if there is an error don't commit changes
and next;
# clean ^M chars ... i'm on Windows
my $m_char = chr(13);
$output_content =~ s/$m_char//g;
# if it is a module or a test, tidy it
if ( $output_path =~ /\.(pm|t)$/ ) {
perltidy(
source => \$output_content,
destination => \my $output_content_tidy,
argv => []
);
# replace content with its tidy version
$output_content = $output_content_tidy;
}
write_file($output_path, $output_content);
}
}
}
File::Spec->curdir
);
Best Regards
MArcos
2011/3/25 Gianluca Casati <casati_gianluca at yahoo.it>:
> Hi all,
>
> I just wrote an article about
>
> Source code generation with Template Toolkit
>
> You can read it here:
> http://perl-node-interface.blogspot.com/2011/03/source-code-generation-with-template.html
>
> Thanks to Marcos to share with me its solution !
>
>
> _______________________________________________
> Milan-pm mailing list
> Milan-pm at pm.org
> http://mail.pm.org/mailman/listinfo/milan-pm
>
>
--
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com
More information about the Milan-pm
mailing list