2013-12-16 55 views
0

儘管對參數中存在的所有標量值都有正確的值,但由於$rc值的原因,此代碼段保持失敗。我不確定$rc值是如何在此計算的。Perl代碼中的錯誤

@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 

請推薦一些東西。

+1

什麼是錯誤信息? – Borodin

+0

在你不理解的行中,'$ rc'被設置爲'system'命令返回值的最低16位。如果出現錯誤,它將作爲錯誤代碼由外部命令返回,並且它不直接成爲該行的一個問題。 –

+0

您確實有一個名爲'$ isql_exe'的變量,或者是變量'$ i'加上字符串'sql_exe'? – TLP

回答

3

$rc = 0xffff & system (@args);是非常錯誤的。

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

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

此代碼好得多:

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"; } 

這是更好,因爲它不使用$rc用於多種用途;它更準確地報告錯誤;閱讀更清晰。

對於$?65280,它會說exited with error 255。退出代碼是特定於給予它們的程序的,並且除零或非零之外通常沒有意義。這就是他們打印錯誤信息的原因。