perl中子程序的运用,以及在子程序中变量进行私有(my)声明的重要性 .
2013-01-01 11:19
441 查看
我们都知道用my可以来定义私有变量,这个可以在很多情况下防止错误的发生,下面我们通过一个例子来看一看。
下面是一个转换程序,也就是简单的把DNA序列中的A转变成T,第一种情况没有使用私有变量。如下:
[plain]
view plaincopyprint?
#下面是一段DNA序列 $DNA=ATTATATAT;#这里是我们的序列 $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; $DNA=$input;#没有使用私有变量 $DNA=~s/A/T/g; return $DNA; }
结果如下:
[plain]
view plaincopyprint?
F:\>perl\a.pl I changed all TTTTTTTTT A to T, and the we get the result TTTTTTTTT F:\>
这里我们发现$DNA的值变成了TTTTTTTTT,而不是以前ATTATATAT。这是因为在子程序中,我们使用了同样的$DNA 变量,而在子程序中它的值已经被改变了。所以输出的时候就是改变以后的值。
下面我们把子程序中的$DNA 进行私有变量声明:
程序如下:
[plain]
view plaincopyprint?
#下面是一段DNA序列 $DNA=ATTATATAT; $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; my $DNA=$input; $DNA=~s/A/T/g; return $DNA; }
结果如下:
[plain]
view plaincopyprint?
F:\>perl\a.pl I changed all ATTATATAT A to T, and the we get the result TTTTTTTTT F:\>
这样就正常了。
当然你可以说,我们在子程序中可以完全不用$DNA这一个变量,就如同下面一样:
[plain]
view plaincopyprint?
#下面是一段DNA序列 $DNA=ATTATATAT; $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; $dna_to_change=$input; $dna_to_change=~s/A/T/g; return $dan_to_change; }
得到的也是正常的结果:
[plain]
view plaincopyprint?
F:\>perl\a.pl I changed all ATTATATAT A to T, and the we get the result F:\>
但是,没有人能够保证你不会一时糊涂,在子程序用了程序中的变量。或者当你第一次使用的时候,可以避免,当你过来几个月以后回过头再来使用的时候,就不能保证完全正确了,所以为了你代码的通用性,我们还是在所有的子程序中使用my私有变量吧!
原文见:http://blog.csdn.net/gaorongchao1990626/article/details/8022712
下面是一个转换程序,也就是简单的把DNA序列中的A转变成T,第一种情况没有使用私有变量。如下:
[plain]
view plaincopyprint?
#下面是一段DNA序列 $DNA=ATTATATAT;#这里是我们的序列 $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; $DNA=$input;#没有使用私有变量 $DNA=~s/A/T/g; return $DNA; }
#下面是一段DNA序列 $DNA=ATTATATAT;#这里是我们的序列 $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; $DNA=$input;#没有使用私有变量 $DNA=~s/A/T/g; return $DNA; }
结果如下:
[plain]
view plaincopyprint?
F:\>perl\a.pl I changed all TTTTTTTTT A to T, and the we get the result TTTTTTTTT F:\>
F:\>perl\a.pl I changed all TTTTTTTTT A to T, and the we get the result TTTTTTTTT F:\>
这里我们发现$DNA的值变成了TTTTTTTTT,而不是以前ATTATATAT。这是因为在子程序中,我们使用了同样的$DNA 变量,而在子程序中它的值已经被改变了。所以输出的时候就是改变以后的值。
下面我们把子程序中的$DNA 进行私有变量声明:
程序如下:
[plain]
view plaincopyprint?
#下面是一段DNA序列 $DNA=ATTATATAT; $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; my $DNA=$input; $DNA=~s/A/T/g; return $DNA; }
#下面是一段DNA序列 $DNA=ATTATATAT; $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; my $DNA=$input; $DNA=~s/A/T/g; return $DNA; }
结果如下:
[plain]
view plaincopyprint?
F:\>perl\a.pl I changed all ATTATATAT A to T, and the we get the result TTTTTTTTT F:\>
F:\>perl\a.pl I changed all ATTATATAT A to T, and the we get the result TTTTTTTTT F:\>
这样就正常了。
当然你可以说,我们在子程序中可以完全不用$DNA这一个变量,就如同下面一样:
[plain]
view plaincopyprint?
#下面是一段DNA序列 $DNA=ATTATATAT; $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; $dna_to_change=$input; $dna_to_change=~s/A/T/g; return $dan_to_change; }
#下面是一段DNA序列 $DNA=ATTATATAT; $result=A_to_T($DNA); print "I changed all $DNA A to T, and the we get the result $result\n\n"; sub A_to_T { my ($input)=@_; $dna_to_change=$input; $dna_to_change=~s/A/T/g; return $dan_to_change; }
得到的也是正常的结果:
[plain]
view plaincopyprint?
F:\>perl\a.pl I changed all ATTATATAT A to T, and the we get the result F:\>
F:\>perl\a.pl I changed all ATTATATAT A to T, and the we get the result F:\>
但是,没有人能够保证你不会一时糊涂,在子程序用了程序中的变量。或者当你第一次使用的时候,可以避免,当你过来几个月以后回过头再来使用的时候,就不能保证完全正确了,所以为了你代码的通用性,我们还是在所有的子程序中使用my私有变量吧!
原文见:http://blog.csdn.net/gaorongchao1990626/article/details/8022712
相关文章推荐
- perl中子程序的运用,以及在子程序中变量进行私有(my)声明的重要性
- perl子程序的运用及子程序中变量私有(my)声明的重要性
- 线性回归、梯度下降以及运用TensorFlow进行简单实现
- 变量以及声明和定义
- C和C++关于变量声明的区别以及一个矛盾的现象
- 第3章 Java语言基础----声明成员变量,对变量进行赋值
- C++编程中变量的声明和定义以及预处理命令解析
- python进阶学习笔记(六)——封装中的 @property,以及私有变量的说明(转载)
- SpringMVC注解@Autowired和@Qualifier 自动注入[根据类型注入] @Autowired 可以对成员变量、方法以及构造函数进行注释, @Qualifier 的
- C语言中的复杂声明和定义,以及如何使用typedef进行简化
- javascript中函数声明、变量声明以及变量赋值之间的关系与影响
- 详解keil采用C语言模块化编程时全局变量、结构体的定义、声明以及头文件包含的处理方法
- JavaScript基础 声明一个整形变量并赋值,两个整形变量进行加法运算
- my,our,local,Perl范围变量声明
- 解决 eclipse 启用增强的自动补全后, 在声明变量时对变量名称进行的扰人的补全行为
- python元组类型的变量以及字符串类型的变量作为参数进行传值
- php——学习笔记,变量声明以及3种赋值方式
- 变量的声明与定义以及关键字extern的用法
- C++ static、const和static const类型成员变量声明以及初始化
- const的用法,以及声明const变量与宏的区别