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?
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?
$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.
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).
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.