http://www.perlmonks.org?node_id=803816

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

Either I was rather clever in the past or, more likely, I had cut-n-pasted code from someone smarter than me, I recently noticed that I am handling a log formatting string in my custom logging function using a hash of subroutine references.

The relevant parts look something like this:

my %meta_char = ( '%' => sub { '%' }, c => sub { $chan }, m => sub { $in_message }, P => sub { $$ }, p => \&_get_package, s => \&_get_subroutine, t => \&_get_timedate, ); ($out_message = $log_format) =~ s/%(.)/$meta_char{$1}->()/ge;

And $out_message is the what gets output.

So if $log_format equals "%t %m", then the time is printed in front of each log message.

The problem is that if $log_format contains a format character that does not exist (for example, "%X") in the %meta_char hash, then Perl does not like that at all.

What I would like is if the code quietly ignored bad log formatting options, but how would I do that? The only thing I can think of is to add some sort of "if exists &sub" logic on the right side of that s// operation. However, Perl is not liking my attempts to do that.

Any other ideas? Thanks much.