2013-04-18 29 views
4

perl如何識別變量的結尾?在打印時,perl如何識別變量的結尾?

例如,下面的代碼:

use warnings; 
my $a = 10; 
print "Value of a is $a:::"; 

輸出:

Use of uninitialized value $a:: in concatenation (.) or string at tryprint.pl line 6. 
Value of a is : 

爲什麼會考慮$ A ::而不是$ A:$或一:::

這作品:

print "Value of a is $a\:::"; 

打印:

Value of a is 10::: 
+5

在一般情況下,凡是能在變量名中使用被解釋爲'$'後一個變量名。如果你想消除歧義,使用大括號;例如,打印'$ a'後面加'b',使用'print'$ {a} b「'(而不是'print'$ ab」'查找可能找不到的變量' $ ab')。 – tripleee 2013-04-18 06:58:15

+0

哦,'Perl':'print「$ parts_ $ of_ $ file_ $ name」':( – gaussblurinc 2013-04-18 08:27:01

回答

7

::用於打印/訪問包/符號表中的變量。例如,要訪問包abc中的標量變量x,Perl使用$abc::x,其中abc是符號表的名稱,x是變量。同樣,當你使用$a:::時,Perl認爲有一個包名爲'a',變量名爲:,因此這些錯誤。

見下面這個例子:

our $a = 10; 
{ 
     my $a=20; 
     print "lexical a is $a \n"; 
     print "Value of a is $main::a"; 
}