I'm using Text::Template this way

file data.var

fields: value pos template: mytemplate_french.tmpl mytemplate_german.tmpl value: the value of pos pos: 3

File mytemplate_french.tmpl

C'est un exemple: {$value} est {$pos}

and the code, calls with code.pl data.var

use strict; use Text::Template; use Config::YAML::Tiny; my @files = map glob($_), @ARGV; my $usage = <<EOF; usage: $0 *.var usage: $0 un.var deux.var ... EOF die $usage unless @files; foreach my $f (@files) { my $config = Config::YAML::Tiny->new( config => $f ); my @template_files = split( / /, $config->get_template ); foreach my $tf (@template_files) { my @fields = split( / /, $config->get_fields ); my $template = Text::Template->new( TYPE => 'FILE', SOURCE => $tf, ', ) or die "Couldn't construct template: $Text::Template::ERROR" +; my %data; for my $f (@fields) { $data{$f} = $config->{$f}; } my $result = $template->fill_in( HASH => \%data); # mytemplate_french.tmpl -> mytemplate_french.txt $tf=~s/\.tmpl//; open( my $out_file, ">$tf.txt" ); if ( defined $result ) { print $out_file $result; print "$tf.txt\n"; } else { print $Text::Template::ERROR; } close $out_file; } }

I would try to catch error in the template where a variable name is misspelled ($ops instead of $pos) and I tried

... my @fields = split( / /, $config->get_fields ); my @vars = map {'$' . $_} @fields; my $str_var = join(" ", @vars); my $template = Text::Template->new( TYPE => 'FILE', SOURCE => $tf, PREPEND => 'use strict; use vars eval($str_var); ', ) or die "Couldn't construct template: $Text::Template::ERROR";

But this does not work since eval is not run in my main package.

This work:
my $template = Text::Template->new( TYPE => 'FILE', SOURCE => $tf, PREPEND => 'use strict; use vars qw($value $pos); ',
But I would like to have something more flexible, since my var files won't have alway the same variable/field names.

Is that possible ?

Thanks

F.

In reply to Text::Template and delayed use vars by frazap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.