在上面的例子中,变量 $x (默认是 undef ),在加法运算中起的作用是0, 在字符串连接过程中表现为空串,而在自增操作中又表现为0。
它也并不是完美的。如果你通过
use warnings
语句开启警告(
推荐使用
),
那么你将在前两个操作中得到
use of unitialized value
警告,但是自增操作却不会。
Use of uninitialized value $x in addition (+) at ... line 6.
Use of uninitialized value $x in concatenation (.) or string at ... line 7.
如果你现在不知道Perl的自增操作也没关系,后面我们会看到, 它在处理计数操作的时候有多么方便。
当然,你可以通过给变量初始化成正确的数值(根据你的需要赋值0或空串)来避免警告, 或者选择性的关闭警告。
我们将在另外单独的文章中讨论这些。
If you have any comments or questions, feel free to post them on the source of this page in GitHub.
Source on GitHub.
Comment on this post