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.


In reply to metacharacter expansion , parse format string similar to log format or printf format by jffry

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.