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.

In reply to Re: Accepting a template string and replacing placeholders the way printf does. by hossman
in thread Accepting a template string and replacing placeholders the way printf does. by ehdonhon

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.