I'd like perl -cw ... to return a non-zero exit status if a compilation warning is emitted.
E.g. suppose a.pm
is the file:
use warnings;
asd;
Then perl -cw a.pm
reports:
Unquoted string "asd" may clash ...
Useless use of a constant in void context ...
a.pm syntax OK
and the exit status is set to 0. I'd like to be able to detect that compilation warnings were emitted - preferably though setting of the exit status.
Set a warning handler in a BEGIN
block (near the top of the script, so this block is parsed before the code that might trigger compile-time warnings), and tweak the exit status in an END
CHECK
block.
use strict;
use warnings;
my $warnings;
BEGIN {
$SIG{__WARN__} = sub { $warnings++; CORE::warn @_ }
}
$used::only::once = 42;
CHECK {
if ($^C && $warnings > 0) {
exit $warnings;
}
}
The $^C
variable is true if and only if you have invoked perl
with the -c
option.
As far as I know, this isn't possible. After all, they are only advisory notices not strictly (no pun intended) errors. Inversely, however, you can promote warnings to fatal errors at runtime if that helps to address your objective.
$ perl -M'warnings FATAL=>q(all)' -cw a.pm ; echo $?
Unquoted string "asd" may clash with future reserved word at a.pm line 2.
255
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With