Praveenks
Praveenks

Reputation: 1494

Error in Perl Code

In spite of having correct values for all the scalar values present in the arguments, This section of code keep getting failed because of the $rc value.I am not sure how the $rc value is getting calculated here.

@args = ("$isql_exe", "-U$user", "-P$password", "-S$server", 
         "-D$database", "-i$tmp_sql_file", "-o$tmp_err_file");  

print $log_file "Truncating stage tables\n";
$rc = 0xffff & system (@args); # <--- what this does?

if ($rc != 0) {
    $rc &= 0x00ff;
    print $log_file "Error Executing SQL command script $rc $?\n";
    $rc = 1;
} ## end if

Please suggest something.

Upvotes: 0

Views: 829

Answers (1)

ikegami
ikegami

Reputation: 386696

$rc = 0xffff & system (@args); is very wrong.

$ perl -E'say system("non-existent")'
-1

$ perl -E'say 0xFFFF & system("non-existent")'
65535

This code is far better:

system(@args);

my $rc = 0;
if    ($? < 0   ) { $rc=1; print $log_file "Error Executing SQL command script: $!\n"; }
elsif ($? & 0x7F) { $rc=1; print $log_file "SQL command script killed by signal ".( $? & 0x7F )."\n"; }
elsif ($? >> 8  ) { $rc=1; print $log_file "SQL command script exited with error ".( $? >> 8 )."\n"; }

It's better because it doesn't use $rc for multiple purposes; it reports error more accurately; and it's much clearer to read.

For a $? of 65280, it will say exited with error 255. Exit codes are specific to the program giving them, and are often meaningless beyond being zero or non-zero. That's why they print error messages.

Upvotes: 3

Related Questions