2011-07-14 54 views
5
perl -e 'use strict;use warnings;$a=2;print $a' 

當我聲明一個沒有our的全局變量時,似乎沒有報告警告,所以何時需要此關鍵字?我們的關鍵字何時需要?

+1

'的perldoc -v「$ a'' –

回答

16

$a$b是特殊情況 - 它們被保留用於sort(例如sort { $a <=> $b }),不應該使用自己的變量名 - 用別的東西再試一次,看看會發生什麼!

11

perlvar:

$a 
    $b  Special package variables when using "sort()", see "sort" in 
      perlfunc. Because of this specialness $a and $b don't need to 
      be declared (using "use vars", or "our()") even when using the 
      "strict 'vars'" pragma. 
相關問題