http://qs321.pair.com?node_id=341079

monsieur_champs has asked for the wisdom of the Perl Monks concerning the following question:

First, I need to make it clear that I'm not sure that this is really usefull, or the Best Way To Do It(tm). So, before you pass trough, please take a minute and think about the problem. Maybe you know a way of doing this that I don't.

I'm building need to build context-driven custom messages for some situations in my system. I'm using Andy Wardley's Template::Toolkit for building the Templates and need to figure out a way to read meta-information from the template, so I can for example do something like this:

Update: Added a little more consistent example code.

[%# Template File %] [% META # META is a kind of a new tag, and stores perl # code for each key needed by the Template. title => sub { use Class::DBI::AutoLoader( %params ); return DATA::Table->retrieve(code=>'A'); } name => sub { return +shift @NAMES || 'Default Name' } %] [% title %] And here goes the template text, and the template [% name %].

# Perl File # %parameters are your template parameters, don't care. my $template = new Template( %parameters ); # data will hold template filling data my %data; map { $data{$_} = eval $template->code_for( $_ ) } $template->needed_k +eys( 'Template.tt' );

In other words, I need an idea for a mechanism that can associate a piece of perl code (in string format) that can be eval()'ed to get the actual value for each variable needed by a template.

Do the Perl Monks have any suggestions of ways of doing this "magic"?

As I need a quick'n'dirty trick, I'll accept any kind of suggestion, provided that there is a minimum change that it works. I just workarrounded it, so now if this is useful in any way, I can try to write a module...

Thank you all in advance for your precious ideas and time!


"In few words, translating PerlMonks documentation and best articles to other languages is like building a bridge to join other Perl communities into PerlMonks family. This makes the family bigger, the knowledge greater, the parties better and the life easier." -- monsieur_champs