2013-02-20 71 views
1
#!/usr/bin/perl 

use strict "vars"; 
use warnings; 
use feature qw(switch); 

use locale; 
use POSIX qw(locale_h); 
setlocale(LC_ALL, "cs_CZ.UTF-8"); 

use constant (
    ERROR_OK => 0, 
    ERROR_CMD => 1, 
    ERROR_INPUT => 2, 
    ERROR_OUTPUT => 3, 
    ERROR_INPUT_FORMAT => 4 
); 

exit ERROR_OUTPUT; 

我仍然得到錯誤「論證‘ERROR_OUTPUT’不是數字,在出口處......」 我怎樣才能使用的出口值,而不是directy使用號碼不變?的Perl - 退出恆

+0

ERROR_OUTPUT或ERROR_OUTPUTS? – aschepler 2013-02-20 15:00:51

+0

@aschepler:thx,編輯 – Krab 2013-02-20 15:01:35

+0

始終使用'use strict;使用警告;'!!! – ikegami 2013-02-20 22:40:22

回答

12

use constant後面的括號改爲curlies。

use constant { 
    ERROR_OK => 0, 
    # etc. 
}; 
+0

奇怪的是,錯誤的聲明不會導致甚至警告。 +1 – 2013-02-20 15:17:28

+0

@ w.k:實際上並沒有錯。嘗試打印或'Data :: Dumper'ing ERROR_OK。 – choroba 2013-02-20 15:30:23

+0

我試過,它逐字打印ERROR_OK。試圖看到使用... – 2013-02-20 15:33:45

4

一個use constant指令應該使用{花括號},不(括號)

use constant { 
    ERROR_OK => 0, 
    ERROR_CMD => 1, 
    ERROR_INPUT => 2, 
    ERROR_OUTPUT => 3, 
    ERROR_INPUT_FORMAT => 4 
}; 
+0

更重要的是,他設置的常量需要大括號。 Parens爲他們記錄的使用案例工作花花公子。 – darch 2013-02-20 17:13:40