The following is an example:
CopyCode The Code is as follows: use strict;
# Here are two Arrays
My @ I = ('1', '2', '3 ');
My @ J = ('A', 'B', 'C ');
# Print them out before processing them 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 use the sub-ProgramProcessing
Reference_sub (@ I, @ J );
Print "in main program after calling Subroutine: I =". "@ I \ n ";
Print "in main program after calling Subroutine: J =". "@ J \ n ";
# The following is a subroutine
Sub reference_sub
{
My (@ I, @ J) = @_;
Print "in Subroutine: I =". "@ I \ n ";
Print "in Subroutine: J =". "@ J \ n ";
# Here we use Pop and shift to process @ _.
Push (@ I, '4 ');
Shift (@ J );
}
The result is 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, then there is no difference between @ I and @ J in @ _, that is, it is confused in @ _ and cannot be distinguished. When we want to obtain the two of them again, we find that @ I = 1 2 3 a B C, while @ J is empty. In many cases, this is obviously not the expected result, so we need to use the pass by reference (translated as "Incoming reference" in Chapter 6 of the big 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 ');
# Print them out before processing them 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 use a subroutine for processing
Reference_sub (\ @ I, \ @ J); # Here we add a backslash (pass byreference)
Print "in main program after calling Subroutine: I =". "@ I \ n ";
Print "in main program after calling Subroutine: J =". "@ J \ n ";
# The following is a subroutine
Sub reference_sub
{
My ($ I, $ J) = @ _; # references are also a special form of data, which are stored as scalar variables in @ _.
Print "in Subroutine: I = ". "@ $ I \ n"; so when we reference them here, we add two symbols before them. @ indicates that this is an array, and $ indicates a secondary reference.
Print "in Subroutine: J =". "@ $ J \ n ";
Print "in Subroutine: the third element is $ J [2] \ n "; # When referencing an element in an array, the first $ and the following J [2] represent the third element in the array, and the second $ represents the secondary reference.
# Here we use Pop and shift to process @ _.
Push (@ $ I, '4 ');
Shift (@ $ J );
}
The result is 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 the subroutine is called, the value of @ I changes: I = 1 2 3 4 and J = B c. That is to say, the push and shift operations on the array in the subroutine play a role in the main program.
Why is this happening?
In general, we declare the variable in the subroutine, that is, it will only play a role in the subroutine and will not change the value in the main program.
Here, because the reference is a real parameter, that is, the value in the main program, rather than its backup, it will be changed accordingly.