Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Text::Template and delayed use vars

by frazap (Beadle)
on May 04, 2018 at 11:50 UTC ( #1214051=note: print w/replies, xml ) Need Help??

in reply to Text::Template and delayed use vars

Yes it's possible, reading Simon Cozens' Advanced Perl, I found this whitch work:
foreach my $f (@files) { my $config = Config::YAML::Tiny->new( config => $f ); my @template_file = split( / /, $config->get_template ); foreach my $tf (@template_file) { my @fields = split( / /, $config->get_fields ); for my $v ( @fields ) { no strict 'refs'; *{"T::$v"}= \$config->{$v}; } my $template = Text::Template->new( TYPE => 'FILE', SOURCE => $tf ) or die "Couldn't construct template: $Text::Template::ERROR" +; my $result = $template->fill_in( PACKAGE => 'T', PREPEND => 'use strict;' ); ...

And in the template file(s) I had to change $value with $T::value, $pos with $T::pos

update In fact, with $T::pos in the template file, I loose the error if the variable name is wrong: $T:ops is just replace by nothing but no error is given. I have to use $pos or $value in the template.

The docs could be made clearer I think


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214051]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-10-20 13:09 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (117 votes). Check out past polls.