A Conversion Program simply converts A in the DNA sequence to T. In the first case, private variables are not used.
Copy codeThe Code is as follows :#! /Bin/perl
# Below is a DNA sequence
$ DNA = ATTATATAT; # Here is our sequence
$ Result = A_to_T ($ DNA );
Print "I changed all $ DNA A to T, and the we get the result $ result \ n ";
Sub A_to_T
{
My ($ input) = @_;
$ DNA = $ input; # No private variables are used
$ DNA = ~ S/A/T/g;
Return $ DNA;
}
The result is 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 $ DNA is changed to TTTTTTTTT, instead of ATTATATAT. This is because we use the same $ DNA variable in the subroutine, and its value has been changed in the subroutine. Therefore, the output result changes the subsequent values.
The $ DNA in the subroutine is declared as a private variable below:
Copy codeThe Code is as follows :#! /Bin/perl
# Below is a DNA sequence
$ DNA = ATTATATAT;
$ Result = A_to_T ($ DNA );
Print "I changed all $ DNA A to T, and the we get the result $ result \ n ";
Sub A_to_T
{
My ($ input) = @_;
My $ DNA = $ input;
$ DNA = ~ S/A/T/g;
Return $ DNA;
}
The result is 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 $ DNA is not a variable in a subroutine, just like the following:
Copy codeThe Code is as follows :#! /Bin/perl
# Below is a DNA sequence
$ DNA = ATTATATAT;
$ Result = A_to_T ($ DNA );
Print "I changed all $ DNA A to T, and the we get the result $ result \ n ";
Sub A_to_T
{
My ($ input) = @_;
$ DNA _to_change = $ input;
$ DNA _to_change = ~ S/A/T/g;
Return $ dan_to_change;
}
The result is 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 for a moment. The program variables are used in the subroutine. Or when you use it for the first time, you can avoid it. When you come back and use it a few months later, it cannot be completely correct, so for the sake of code versatility, use my private variables in all subroutines.