编写下面的简单示例来说明如何处理如下类型的错误提示:
Global symbol "$c" requires explicit package name at *.pl line 8.
Execution of *.pl aborted due to compilation errors.
#############Code Starts###############
#!/usr/bin/perl -w
use strict;
$a=0377;
$b=0x12;
$c=$a+$b; #key point;
print $c,"\n";
#############Code Ends###############
这时候直接运行是编译不通的,会出现开头说道的那种错误。
关键在于有$c的那2行:变量的作用域问题和use strict的使用。
我们使用my来声明$c,即可改正问题:
把"
$c=$a+$b;" 改为"my
$c=$a+$b;",再次运行,成功!
use strict的用法:
1.什么场合要用 use strict
当你的程序有一定的行数时,尤其是在一页放不下时,或者是你找不到发生错误的原因时。
2.为什么要用 use strict?
众多的原因之一是帮你寻找因为错误拼写造成的错误。比如错误使用了'$recieve_date' 变量,但实际上你在程序中已声明的是 '$receive_date' 变量,这个错误就很难发现。同样,use strict 迫使你把变量的范围缩到最小,使你不必担心同名变量在程序的其它部份发生不良作用。(尽管这是 my 的功能,但是如果你使用 use strict 的话,它会强迫你用 my 声明变量,来达到上述目的)。
3.用use strict麻烦吗?
不麻烦,只要在你的脚本的开始加上11个字符而已!(use strict;), 另外在整个程序中用my 声明变量。
在你的脚本的开头 '#!/usr/local/bin/perl' 后面加上这句就行:
use strict;
4.程序出错了,该怎么办?
常见的错误信息一般如下:(前面已经解决过了)
Global symbol "$c" requires explicit package name at *.pl line 8.