in reply to metacharacter expansion , parse format string similar to log format or printf format

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.

  • Comment on Re: metacharacter expansion , parse format string similar to log format or printf format
  • Download Code

Replies are listed 'Best First'.
Re^2: metacharacter expansion , parse format string similar to log format or printf format
by jffry (Hermit) on Feb 18, 2008 at 22:13 UTC
    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.