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

Hello,

I'm working on a small API for a project that I'm involved in at the moment. One of the things I want to be able to do is allow somebody to send me some template text along with placeholders for special information that my object knows about, and then return the text with the required information substituted in. Very similar to the way apache allows you to specify custom log formats.

It seems to me that using placeholders the way that printf does ( % then some identifier, perhaps with some formatting options) seems to be a standard way of doing this.

My main question: is there any module out there that specializes in parsing these kinds of template strings, or do I pretty much have to start from the ground up?

If I do have to start from the ground up, then I'm thinking that initially, I'm going to try something simple like this:

my $template_text =~ s/\%g/$g_var/g;

There are a ton of problems with this:

I really appreciate any assistance with this, especially if somebody can tell me that this has already been written! :)

  • Comment on Accepting a template string and replacing placeholders the way printf does.
  • Download Code

Replies are listed 'Best First'.
Re: Accepting a template string and replacing placeholders the way printf does.
by grep (Monsignor) on Jan 13, 2002 at 06:43 UTC
    Although Template Toolkit is usually used for HTML templates, it sounds like a perfect fit for your application. TT uses [% tag %] syntax and it really does not care what is around it. It has some simple logic and looping constructs, and is very easy to learn and implement.

    grep
    grep> cd pub grep> more beer
Re: Accepting a template string and replacing placeholders the way printf does.
by hossman (Prior) on Jan 13, 2002 at 07:24 UTC
    You bring up a lot of good arguments against using printf style arguments, but i wouldn't rule them out.

    if accepting args that look like printf is what you want, then i say "go for it" because implimenting it isn't really that hard.

    It really isn't scalable, if I end up with 50 possible things to substitute, I have to have 50 reg exp's.
    Not true, you just have to change the way your deal with your format string (you don't need one pass per possible substitutions -- just one pass per arg (or per substitution used depending on how you do it)

    It doesn't deal with escaping characters. My regular expression experience is really limited in this area. How can I make '%g' substitute to $g_var and '\\%g' substitute to '\' . $g_var and make '\%g' substitute to '%g' with a replacement expression?
    Acctually, printf style args are usually escaped by saying that "%%" evaluates to "%", which you can handle with a negative lookback assertion.

    My gut instinct just tells me this is wrong.
    My gut disagree's with your gut.

    Use the code below as a start to get you on the right track. my_printf is acctually very simple -- allthough if you want modifiers on your tokens: (ie: "%0.2d") the regex will get a little more complicated.

    The real work is in replace_arg (which could definitely be improved on for large numbers of args by using a hashtable or something...

    #!/usr/local/bin/perl -wl use strict; sub my_printf { my $text = shift; my $arg; while ($arg = shift) { $text =~ s/(?<!\%)\%([^\%])/&replace_arg($1,$arg)/e; } $text =~ s/%%/%/g; return $text; } sub replace_arg { # takes in a replacement 'code' and an arg # converts the arg, per the code my ($code, $arg) = @_; if ('a' eq $code) { # %a means flat substitution return $arg; } elsif ('b' eq $code) { # %b means length of the arg return length $arg; } # else.. return "humina:$code?"; } print my_printf("foo %a bar %b", "baz", "baz"); print my_printf("oy %%9"); print my_printf("the %a is %% %b", "probability", "foo bar"); __END__ foo baz bar 3 oy %9 the probability is % 7
    Update: I just realized that i was handling %% in a very complicated and wrong way. Rather then completely reworking the solution to loop over the '%'s (instead of over the args) i just tweaked it a little bit to do (approximately) the right thing.
      My conscience caught up with me, here is a much cleaner way of doing it...
      #!/usr/local/bin/perl -wl use strict; sub my_printf { my $text = shift; $text =~ s/\%(.)/&replace_arg($1,\@_)/ge; return $text; } sub replace_arg { # takes in a replacement 'code' and an arg list ref # converts the code, poping from the arg list as needed # if you have more then 3 possible codes, you might want to # impliment this as a hash of code refs (aka: closures) my ($code, $args) = @_; if ('a' eq $code) { # %a means flat substitution return shift @{$args}; } elsif ('b' eq $code) { # %b means length of the arg return length shift @{$args}; } elsif ('%' eq $code) { # %% means % return '%'; # note: no shift } # else... return "humina:$code?"; } print my_printf("foo %a bar %b", "baz", "baz"); print my_printf("oy %%9"); print my_printf("the %a is %% %b", "probability", "foo bar"); print my_printf("ignoring %%100 of the unused args", "unused arg"); __END__ foo baz bar 3 oy %9 the probability is % 7 ignoring %100 of the unused args