[Milan-pm] Source code generation with Template Toolkit
Gianluca Casati
casati_gianluca at yahoo.it
Fri Mar 25 11:11:31 PDT 2011
Thank you again for your useful hints during our meetings, I really learned a
lot of Perl !
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.
Probably this shorter code works pretty good on Linux, using paths instead of
references ...
use strict;
use warnings;
use File::Find;
use File::Spec;
use Template;
my $template_config = {
STRICT => 1,
DEBUG => 1,
TRIM => 1
};
my $template = Template->new($template_config);
find(
{
wanted => sub {
return unless /\.tt2$/;
# naming convention: /path/to/my/file.ext.tt2
# is template file for /path/to/my/file.ext
my $template_path = $File::Find::name;
my $output_path = $template_path;
$output_path =~ s/\.tt2$//;
# process templates
$template->process( $template_path, {}, $output_path,
{ binmode => 1 } )
or warn $template->error
# but if there is an error don't commit changes
and next;
}
},
File::Spec->curdir
);
... but if you then want to tidy code, using references it's worth it.
Well, there are more than 100 ways to do it (and latins do it better :-) .
________________________________
Da: marcos rebelo <oleber at gmail.com>
A: milan-pm at pm.org
Inviato: Ven 25 marzo 2011, 15:22:31
Oggetto: Re: [Milan-pm] Source code generation with Template Toolkit
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
>l
>
> 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
_______________________________________________
Milan-pm mailing list
Milan-pm at pm.org
http://mail.pm.org/mailman/listinfo/milan-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/milan-pm/attachments/20110325/9707f267/attachment.html>
More information about the Milan-pm
mailing list