I am using a Perl script to construct a spark-submit command that will trigger a long running spark job on a Hadoop cluster, then using system() to execute the command. Sometimes, system() returns undef instead of a proper return code, resulting in inaccurate logic flow since undef == 0 evaluates to true.
My current code:
#!/usr/local/bin/perl5.12 -w
#...
my $exitCode = system("$sparkSubmitCmd");
sleep(10);
if ($exitCode == 0) {
$log->info("$jobName run succeeded.n");
#Logic if run succeeded
} else {
$log->error("$jobName run failed.n");
#Logic if run failed
}
Produced this log with a failed underlying run:
Argument "" isn't numeric in numeric eq (==) at /usr/local/bfm/etc/SCRIPT_NAME.pl line #.
JOB_NAME run succeeded
Where the line # pointed to the $exitCode == 0 if statement.
I know I can switch how undef is treated by adding defined($exitCode) in the if statement, but I don’t want to report false negatives or false positives. This issue has been very difficult to reproduce as it only happens occasionally. I have not been able to determine if a successful execution will ever return undef or not.
Sidestepping the question of whether it’s better to report false negatives or false positives, how do I avoid undef entirely? If that’s not possible, how do I construct the script to determine the run state even in the case of an undef?