博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Being Strict with Your Variables
阅读量:2457 次
发布时间:2019-05-10

本文共 1579 字,大约阅读时间需要 5 分钟。

 

In the last chapterthe use of modules to implement pragmas was discussed. One very useful pragma to aid in debugging is use strict;. This statement does two things:

  • Forces you to use the my() function to declare all variables. When all variables have a local scope, you avoid problems associated with unintentionally changing the value of a variable in a function.
  • Ensures that you can't use accidental symbolic dereferencing. This topic was not covered in Chapter on References, because it is relatively advanced. If you use the dereferencing techniques shown in that Chapter you won't need to worry about this requirement.

 

When the strict pragma is used, your script will not compile if the preceding two rules are violated. For example, if you tried to run the following lines of code, debug3.pl

use strict; $foo = { }; $bar = 5; print("$foo/n"); print("$bar/n");

you would receive these error messages:

Global symbol "foo" requires explicit package name at test.pl line 3. Global symbol "bar" requires explicit package name at test.pl line 4. Global symbol "foo" requires explicit package name at test.pl line 6. Global symbol "bar" requires explicit package name at test.pl line 7. Execution of test.pl aborted due to compilation errors.

In order to eliminate the messages, you need to declare $foo and $bar as local variables, like this, debug4.pl:

use strict; my($foo) = { }; my($bar) = 5; print("$foo/n"); print("$bar/n");

The my() function makes the variables local to the main package.

In the next section, you see how to use the debugger to step through your programs.

转载地址:http://lrjhb.baihongyu.com/

你可能感兴趣的文章
ntp symmetric_Python使用示例设置symmetric_difference_update()方法
查看>>
物联网互联收费_联网| 互联网络能力问答 套装1
查看>>
kotlin中判断字符串_Kotlin程序计算字符串中每个字符的出现
查看>>
math asin_Java Math类静态double asin(double d)方法及示例
查看>>
如何从JavaScript中的数组替换元素?
查看>>
Python中的字符串文字前的'b'字符做什么?
查看>>
程序如何检查堆栈溢出_通过使用堆栈检查平衡的括号(C ++程序)
查看>>
jquery-dom_jQuery DOM
查看>>
golang 变量_Golang中的变量
查看>>
ruby元编程_Ruby中的套接字编程
查看>>
不允许相邻
查看>>
计算机图形学图形旋转_计算机图形学中的旋转
查看>>
oracle中dbms_DBMS中的关系演算
查看>>
rip1 rip2_RIP的完整形式是什么?
查看>>
Python中的Numpy数组索引
查看>>
二叉树右视图_二叉树的右视图
查看>>
ruby 集合 分组_在Ruby中找到集合的长度
查看>>
sizeof函数_PHP sizeof()函数与示例
查看>>
python函数示例_abs()函数以及Python中的示例
查看>>
python函数示例_Python中带示例的构造函数
查看>>