Here is an example:
Copy Code code as follows:
Use strict;
#这里是两个数组
My @i = (' 1 ', ' 2 ', ' 3 ');
My @j = (' A ', ' B ', ' C ');
#在进行处理之前, we'll print them out and look at them.
Print "In the main program before calling subroutine:i=". @i\n ";
Print "In the main program before calling subroutine:j=". @j\n ";
#然后我们通过子程序进行处理
Reference_sub (@i,@j);
Print "in Main, after calling subroutine:i=". @i\n ";
Print "in Main, after calling subroutine:j=". @j\n ";
#下面是子程序
Sub Reference_sub
{
My (@i,@j) =@_;
Print "in subroutine:i=". @i\n ";
Print "in subroutine:j=". @j\n ";
#这里我们对 @_ are processed using POPs, and shift.
Push (@i, ' 4 ');
Shift (@j);
}
The results obtained are as follows:
Copy Code code 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, after calling Subroutine:i=1 2 3
In main, after calling Subroutine:j=a b C
F:\>
In this case, subroutine has two parameters @i and @j, when we pass these two parameters to the subroutine, the subroutine put them all into the built-in array @_, and then in @_ @i and @j different, that is, in the @_ confused, indistinguishable. When we wanted to get the two of them back, we found that @i=1 2 3 a B C, and @j was empty. In many cases this is obviously not the result we want to get, so we need to use pass by reference (translated as "incoming references" in the sixth chapter of the Great Camel book)
We just make a change in the code, which is
The procedure is as follows:
Copy Code code as follows:
Use strict;
#这里是两个数组
My @i = (' 1 ', ' 2 ', ' 3 ');
My @j = (' A ', ' B ', ' C ');
#在进行处理之前, we'll print them out and look at them.
Print "In the main program before calling subroutine:i=". @i\n ";
Print "In the main program before calling subroutine:j=". @j\n ";
#然后我们通过子程序进行处理
Reference_sub (\@i,\@j); #在这里我们添加了反斜线, that is, pass byreference (incoming quote, translation in the sixth chapter of the Great Camel book)
Print "in Main, after calling subroutine:i=". @i\n ";
Print "in Main, after calling subroutine:j=". @j\n ";
#下面是子程序
Sub Reference_sub
{
My ($i, $j) =@_ #引用也是一种特殊的数据形式, they are stored as scalar quantities in @_
Print "in subroutine:i=". @ $i \ n "; So when we reference them here, we add two symbols in front of them, @ This is an array, and $ represents a two-time reference.
Print "in subroutine:j=". @ $j \ n ";
print ' in subroutine:the third the element is $ $j [2]\n '; #当引用数组中的一个元素的时候第一个 $ and later j[2] represent the third element in the array, and the second $ represents two references
#这里我们对 @_ are processed using POPs, and shift.
Push (@ $i, ' 4 ');
Shift (@ $j);
}
The results are as follows:
Copy Code code 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 the element is C
In main, after calling Subroutine:i=1 2 3 4
In main program after calling Subroutine:j=b C
F:\>
We can look at the final result,
The value of @i changed after the subroutine was invoked: I=1 2 3 4 and J=b C. That is, the push and shift operations of the arrays in the subroutine play a role in the main program.
Why is this happening?
In general, we declare a variable in a subroutine that will only work in a subroutine and 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, so it will be changed accordingly.