views:

118

answers:

4

What is the best way to log an error when testing in Perl? I prefer a one-liner solution such as

return 0 if $var!=3 and $logger->error("Var is not 3"))

Can something like the example shown be implemented?

+2  A: 
$logger->error("Var is not 3"), return 0 if $var != 3;

This is taking advantage of Perl's support for the comma operator, which allows you to write an expression where parts to the left are evaluated but their value ignored, with the final value of the expression being the rightmost part.

chaos
+2  A: 

You could always do:

$logger->error("Var is not 3") and return 0 if $var != 3

which takes advantage of and's low precedence (and if's even lower precedence).

Chris Simmons
A: 

I prefer using carp/croak/confess if it's just me debugging, but if you're doing it for production code, maybe try Log::Report.

So something like: $var == 3 or confess("var != 3!") is what I'd use if I wanted a one liner that prints an error and a stacktrace.

plastic chris
A: 

Log4Perl may do what you'd like. This is the Perl clone of Log4J. It has fine grain dynamic log filtering configuration.

jeje