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

Here is my solution to the common problem of parsing a format string typical of the formats used in log or printf specifications.

For example, to specify '[%t] [%l] %m' and have the parsed output be '[23:21] [err] Something bad happened.'

Simple string substitution is not enough, however. I need the substitution text to be the result of a function. :sigh: Actually, looking at my problem again, this isn't necessarily true, but I've been banging away at it under this assumption for so long, that I still want to see what other folks think.

my @tokens = split '', $ARGV[0]; my %meta = ( 't' => \&time, 'l' => \&log_level, 'm' => \&message, ); my $max_iter = scalar @tokens; TOKEN: while ($indx < $max_iter) { if ($tokens[$indx] eq '%') { $indx++; for my $key (keys %meta) { if ($tokens[$indx] eq $key) { $output = $output . &{ $meta{$key} }; next TOKEN; } } $output = $output . '%' . $tokens[$indx]; next TOKEN; } $output = $output . $tokens[$indx]; } continue { $indx++; } print "\n$output\n";

I just know that there must be a more elegant solution using substr, map, or s//, but all of these solutions eluded me.


In my regexp attempt, I stole from the Q&A node Subroutines in a regex? and tried this.

$format =~ s/(%m)/&message()/;
But this input 'the message is: %m' came out like this the message is: &message()

Conceptually, substr looked like a better fit, but I could not determine how to allow for the same metacharacter to be used more than once, even though there is not (yet) a valid reason for this.


The problem I ran into trying to use map was how to preserve the whitespace in the format string. If I didn't care about whitespace, I could have split the format string on whitespace and used map, but the whitespace is itself part of the format specification.

Replies are listed 'Best First'.
Re: metacharacter expansion , parse format string similar to log format or printf format
by Corion (Patriarch) on Feb 18, 2008 at 21:48 UTC

    You want the World's Simplest Templating Engine, s///e:

    my $tokens = $ARGV[0]; my %meta = ( 't' => \&time, 'l' => \&log_level, 'm' => \&message, '%' => sub { '%' }, # So you can put "50%%" into your template and + have it render as "50%" ); ($output = $tokens) =~ s/%(.)/$meta{$1}->()/ge;

    That way, Perl will do all the parsing for you. See perlre.

      Ahh! The key part there is the 'e' flag to the s// regexp operator.

      FYI, the 'e' flag documentation is easier to find at the s/PATTERN/REPLACEMENT/msixpogce section of the perlop page.

      Thanks much.