in reply to Accepting a template string and replacing placeholders the way printf does.

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.
  • Comment on Re: Accepting a template string and replacing placeholders the way printf does.
  • Download Code

Replies are listed 'Best First'.
Re: Re: Accepting a template string and replacing placeholders the way printf does.
by hossman (Prior) on Jan 13, 2002 at 13:49 UTC
    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