A conversion program that simply converts a A in the DNA sequence to T, the first of which does not use a private variable.
Copy Code code as follows:
#!/bin/perl
#下面是一段DNA序列
$DNA =attatatat; #这里是我们的序列
$result =a_to_t ($DNA);
Print "I changed all $DNA A to T, and the ' we get ' result $result \ n \ nthe";
Sub a_to_t
{
My ($input) =@_;
$DNA = $input; #没有使用私有变量
$DNA =~s/a/t/g;
return $DNA;
}
The results are as follows:
f:\>perl\a.pl
I changed all ttttttttt A to T, and the "we get the" result ttttttttt
F:\>
Here we find that the value of the $dna is changed to TTTTTTTTT, not the previous attatatat. This is because in the subroutine we use the same $dna variable, and its value has been changed in the subroutine. So the output time is to change the value of the future.
The following $DNA in the program to make a private variable declaration:
Copy Code code as follows:
#!/bin/perl
#下面是一段DNA序列
$DNA =attatatat;
$result =a_to_t ($DNA);
Print "I changed all $DNA A to T, and the ' we get ' result $result \ n \ nthe";
Sub a_to_t
{
My ($input) =@_;
my $DNA = $input;
$DNA =~s/a/t/g;
return $DNA;
}
The results are as follows:
f:\>perl\a.pl
I changed all Attatatat A to T, and the "we get the" result ttttttttt
F:\>
This is normal.
Of course, you can say that in a subroutine you can simply not $dna this variable, just like the following:
Copy Code code as follows:
#!/bin/perl
#下面是一段DNA序列
$DNA =attatatat;
$result =a_to_t ($DNA);
Print "I changed all $DNA A to T, and the ' we get ' result $result \ n \ nthe";
Sub a_to_t
{
My ($input) =@_;
$dna _to_change= $input;
$dna _to_change=~s/a/t/g;
return $dan _to_change;
}
The results are also normal:
f:\>perl\a.pl
I changed all Attatatat A to T, and the "we get the" result
F:\>
However, no one can guarantee that you will not be confused in a subroutine using the variables in the program. Or when you first use it, you can avoid it, and when you come back a few months later and use it, you can't guarantee that it's completely correct, so for the sake of the versatility of the code, use my private variable in all the subroutines.