The use of Perl subroutines and the importance of the variable private (my) declaration in subroutines _perl

Source: Internet
Author: User

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.

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.