in reply to Moose based logging question

You could do something like this:

my $logger; use constant DEBUGGING => !!$ENV{DEBUG}; sub log_this (&) { DEBUGGING or return; $logger ||= Logger->new; $logger->log($_) for shift->(); } log_this { "Hello world" }; log_this { "Hello", "world" }; log_this { my $data = Expensive->method_call(); "DATA: $data"; };

If the $DEBUG environment variable is false, log_this will return quickly without evaluating the contents of the code block. This still has the overhead of the sub call to log_this itself, but that overhead will be fairly small.

As a bonus, you could use something like Keyword::Simple to alter the parsing of log_this to avoid having even the overhead of that redundant sub call when $DEBUG is false. If you write your custom parser carefully, so that it has no real effect on the syntax of the log_this keyword but just causes it to be no-opped as appropriate, then the custom parsing could be dropped in for a speed-up only on those versions of Perl that support it (5.14+), but it would still work on older Perls.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: Moose based logging question
by jandrew (Chaplain) on Feb 09, 2014 at 21:24 UTC

    tobyink++,

    Thanks for the pointer to Keyword::Simple as well.