perl中子程序的运用,以及在子程序中变量进行私有(my)声明的重要性 .
来源:互联网 发布:java 线程池自动关闭 编辑:程序博客网 时间:2024/06/05 09:19
我们都知道用my可以来定义私有变量,这个可以在很多情况下防止错误的发生,下面我们通过一个例子来看一看。
下面是一个转换程序,也就是简单的把DNA序列中的A转变成T,第一种情况没有使用私有变量。如下:
- #下面是一段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;}
结果如下:
- F:\>perl\a.pl
- I changed all TTTTTTTTT A to T, and the we get the result TTTTTTTTT
- F:\>
F:\>perl\a.plI changed all TTTTTTTTT A to T, and the we get the result TTTTTTTTTF:\>
这里我们发现$DNA的值变成了TTTTTTTTT,而不是以前ATTATATAT。这是因为在子程序中,我们使用了同样的$DNA 变量,而在子程序中它的值已经被改变了。所以输出的时候就是改变以后的值。
下面我们把子程序中的$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;
- }
#下面是一段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;}
结果如下:
- F:\>perl\a.pl
- I changed all ATTATATAT A to T, and the we get the result TTTTTTTTT
- F:\>
F:\>perl\a.plI changed all ATTATATAT A to T, and the we get the result TTTTTTTTTF:\>
这样就正常了。
当然你可以说,我们在子程序中可以完全不用$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_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;}
得到的也是正常的结果:
- F:\>perl\a.pl
- I changed all ATTATATAT A to T, and the we get the result
- F:\>
F:\>perl\a.plI changed all ATTATATAT A to T, and the we get the resultF:\>
但是,没有人能够保证你不会一时糊涂,在子程序用了程序中的变量。或者当你第一次使用的时候,可以避免,当你过来几个月以后回过头再来使用的时候,就不能保证完全正确了,所以为了你代码的通用性,我们还是在所有的子程序中使用my私有变量吧!
原文见:http://blog.csdn.net/gaorongchao1990626/article/details/8022712
- perl中子程序的运用,以及在子程序中变量进行私有(my)声明的重要性
- perl中子程序的运用,以及在子程序中变量进行私有(my)声明的重要性 .
- perl写子程序持久性私有变量state声明符报错的原因
- PERL语言入门:第四章:子程序(函数):参数与子程序的私有变量
- perl的子程序
- PERL语言的子程序
- perl 子程序的使用
- perl的子程序
- 子程序的运用
- my,our,local,Perl范围变量声明
- my,our,local,Perl范围变量声明
- my,our,local,Perl范围变量声明
- my,our,local,Perl范围变量声明
- perl中子程序中参数的两种引用(传递)方式介绍
- 子程序中的私有变量
- perl 函数传递以及my的使用
- linux bash shell 子程序、函数的简单声明以及调用
- 在VC中调用PERL子程序
- MapReduce Design Patterns(chapter 2 (part 3))(四)
- 绕过防盗链
- EJB3.0学习笔记-----Stateful Session Bean的原理
- Java Design Demo--枚举类型--避免嵌套过多
- 控制控件上下左右移动
- perl中子程序的运用,以及在子程序中变量进行私有(my)声明的重要性 .
- java读取文件夹中的文件夹和文件基本流程
- LA - 3644 - X-Plosives
- 向Web开发人员推荐12款优秀的 Twitter Bootstrap 组件和工具
- 编译错误"/usr/bin/ld: cannot find -lz"
- [Perl工具篇] 安装Strawberry Perl与第一个Perl程序(Padre下)
- ExtJS 主要组件
- 驱动该如何入门 关于file_operations和Linux设备模型
- .NET 中 对窗口中某一控件全屏显示的方法二则