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

Hi Monks!

I'm trying to do something like this:
$my_template_string = $configuration->{my_template_string}; $string = some_format_function($my_template_string, {var1=>"Guybrush", + var2=>"Purcell"});
where $my_template_string is, for example:
"bar ${var1} foo foo ${var2} bar bar BAAAR!"
Do you know such a sort of function or a similar way to approach this?

Thanks,
mc

Replies are listed 'Best First'.
Re: Template strings
by Corion (Patriarch) on Jul 05, 2005 at 09:07 UTC

    The low-fi way would be some cleverly crafted search-and-replace:

    sub fill_template { my ($string,$values) = @_; $string =~ s!\$\{(.*?)\}!$values->{$1}!eg; $string };

    But in most cases, you will want to simply use one of the various already existing templating systems, depending on if you only want to template HTML-like files or also other stuff:

Re: Template strings
by chas (Priest) on Jul 05, 2005 at 11:36 UTC
Re: Template strings
by dirac (Beadle) on Jul 05, 2005 at 09:24 UTC
    #!/usr/bin/perl -w
    use strict;
    my $my_template_string = 'bar ${var1} foo foo ${var2} bar bar BAAAR!';
    my %V = (var1=>"Guybrush", var2=>"Purcell");
    (my $string = $my_template_string ) =~ s/\$\{(.*?)\}/$V{$1}/eg;
    print $string;
    
Re: Template strings
by trammell (Priest) on Jul 05, 2005 at 13:41 UTC
    Let's not forget sprintf():
    my $template = 'bar %s foo foo %s bar bar BAAAR!'; my $out = sprintf $template, 'Guybrush', 'Purcell';

    Update: This is also a FAQ.

    Update 2: This is also the Cookbook recipe of the day on perl.com. What a coincidence!

Re: Template strings
by davidrw (Prior) on Jul 05, 2005 at 12:43 UTC
    Mentioned above already, but thought I'd give an example of Template Toolkit to illustrate how similiar it is to your request:
    use Template; my $template = Template->new(); my $my_template_string = 'bar [% var1 %] foo foo [% var2 %] bar bar BA +AAR!'; my $vars = {var1=>"Guybrush", var2=>"Purcell"}; my $string; $template->process(\$my_template_string, $vars, \$string);
    Note that TT it is very powerful and supports all kinds (IF, FOREACH, BLOCK, etc) of logic. http://www.template-toolkit.org
Re: Template strings
by simonm (Vicar) on Jul 05, 2005 at 17:45 UTC
    You can use Text::MicroMason:
    use Text::MicroMason::Functions qw( execute ); $template = "bar <% $ARGS{var1} %> foo <% $ARGS{var2} %> bar BAAAR!" +; $output = execute( $template, var1=>"Guybrush", var2=>"Purcell" );

    If it will be used repeatedly, you can pre-compile your template for faster execution:

    use Text::MicroMason::Functions qw( compile ); $template = "bar <% $ARGS{var1} %> foo <% $ARGS{var2} %> bar BAAAR!" +; $subroutine = compile( $template ); $output = $subroutine->( var1=>"Guybrush", var2=>"Purcell" );

    There's also an object-oriented interface:

    use Text::MicroMason; $interpreter = Text::MicroMason->new(); $template = "bar <% $ARGS{var1} %> foo <% $ARGS{var2} %> bar BAAAR!" +; $subroutine = $interpreter->compile( $template ); $output = $subroutine->( var1=>"Guybrush", var2=>"Purcell" );

    You can swap in a different syntax if desired:

    use Text::MicroMason; $interpreter = Text::MicroMason->new( -TextTemplate ); $template = "bar { $var1 } foo { $var2 } bar BAAAR!"; $subroutine = $interpreter->compile( $template ); $output = $subroutine->( var1=>"Guybrush", var2=>"Purcell" );
    use Text::MicroMason; $interpreter = Text::MicroMason->new( -HTMLTemplate ); $template = "bar <TMPLVAR NAME="var1"> foo <TMPLVAR NAME="var2"> bar + BAAAR!"; $subroutine = $interpreter->compile( $template ); $output = $subroutine->( var1=>"Guybrush", var2=>"Purcell" );