The Application of perl subprograms and the importance of variable private (my) declarations in subprograms

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.