Introduction of Two Reference (Transfer) Ways of Parameters in Perl Neutron Program

Time:2019-7-7

Here is an example:

Copy codeThe code is as follows:
use strict;
# Here are two arrays
my @i =(‘1′,’2′,’3’);
my @j =(‘a’,’b’,’c’);

# Before processing, we print them out and see what they look like.
print “In main program before calling subroutine:i=”.”@i\n”;
print “In main program before calling subroutine:j=”.”@j\n”;

# Then we deal with it through subroutines.
reference_sub(@i,@j);
print “In main program after calling subroutine:i=”.”@i\n”;
print “In main program after calling subroutine:j=”.”@j\n”;
# Here are the subroutines
sub reference_sub
{
 my (@i,@j)[email protected]_;
 print “In subroutine:i=”.”@i\n”;
 print “in subroutine:j=”.”@j\n”;

# Here we use pop and shift to handle @_
 push(@i,’4′);
 shift(@j);
}

The results are as follows:

Copy codeThe code is as follows:
F:\>perl\a.pl
In main program before calling subroutine:i=1 2 3
In main program before calling subroutine:j=a b c
In subroutine:i=1 2 3 a b c
in subroutine:j=
In main program after calling subroutine:i=1 2 3
In main program after calling subroutine:j=a b c
F:\>

In this example, there are two parameters @i and @j in the subroutine. When we pass these two parameters to the subroutine, the subroutine puts them all in the built-in array @ and then the difference between @i and @j in @ has disappeared. That is to say, they are confused in @ and cannot be distinguished. When we want to get both of them again, we find that @i = 123 a B C and @j is empty. In many cases this is obviously not the result we want, so we need to use pass by reference (translated as “pass by reference” in Chapter 6 of the Great Camel Book).
We just make a change to the code, that is, in
The procedure is as follows:

Copy codeThe code is as follows:
use strict;

# Here are two arrays
my @i =(‘1′,’2′,’3’);
my @j =(‘a’,’b’,’c’);

# Before processing, we print them out and see what they look like.
print “In main program before calling subroutine:i=”.”@i\n”;
print “In main program before calling subroutine:j=”.”@j\n”;

# Then we deal with it through subroutines.
Reference_sub( i, j); Here we add a backslash, which is pass by reference.
print “In main program after calling subroutine:i=”.”@i\n”;
print “In main program after calling subroutine:j=”.”@j\n”;
# Here are the subroutines
sub reference_sub
{
My ($i, $j) = @; References are also a special form of data that are stored as scalar variables in @.
Print “In subroutine: i=”. “@$i n”; so when we quote them here, we add two symbols in front of them, @ for an array, $for a secondary reference.
 print “in subroutine:j=”.”@$j\n”;
Print “In subroutine: the third element is $$j[2] n”; # When referring to an element in an array, the first $and the subsequent j[2] represent the third element in the array, and the second $represents the second reference.
# Here we use pop and shift to handle @_
 push(@$i,’4′);
 shift(@$j);
}

The results are as follows:

Copy codeThe code is as follows:
F:\>perl\a.pl
In main program before calling subroutine:i=1 2 3
In main program before calling subroutine:j=a b c
In subroutine:i=1 2 3
in subroutine:j=a b c
In subroutine:the third element is c
In main program after calling subroutine:i=1 2 3 4
In main program after calling subroutine:j=b c
F:\>

We can observe the final result.
After calling the subroutine, the value of @i changes: I = 1 234 and j = B C. That is to say, push and shift operations on arrays in subroutines play a role in the main program.
Why does this happen?
Generally speaking, we make my declaration on variables in subroutines, that is to say, it only works in subroutines without changing the values in the main program.
In this case, because the reference is the real parameter, that is, the value in the main program, rather than its backup, it will be changed accordingly.