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.
But this input 'the message is: %m' came out like this the message is: &message()$format =~ s/(%m)/&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.
In reply to metacharacter expansion , parse format string similar to log format or printf format by jffry
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |