We already know that the Perl subroutine parameter transmission mode is similar to the reference (or address) passed. If the value of the array is changed in the SUB, the value of the arguments is also changed. Note: When the argument is used as a variable, the value of the parameter can change. Similar & test (1, "abc"); SUB Test {$ _ [0] ; ....} The code will be reported: Modification of a read-only value attempted at d: /perlexec/noname11.pl line 6. It seems that the parameter transmission method of Perl is really special, it is different from any language. Since the delivery is a reference, can an array can be used as a parameter? Can its value change? Can you append the delete element? What impact does the original array will cause? Sample03.pl #! / usr / bin / perl @arr = (A, B, C, D, E); Print "main: / n"; foreach (@arr) {print "$ _ / n";} & test (@arr); Foreach (@arr) {print "$ _ / n";} Sub test {print "/ nsub: / n"; shift @_; $ _ [0] = "g"; $ _ [10 ] = "h"; push @_, "f";} Execution: main: abcde sub: AGCDE array still maintains the original size constant, the element has no increase in increase, the content of the second element is changed " g. The system maintains an array @_, accommodates all parameters, and the elements are references. The additional deletion operations of @_ are only valid. If I want all the operations to array to reflect the argument? Let SUB returns @_, re-pay the real parameters. Sample04.pl #! / usr / bin / perl
@arr = (A, B, C, D, E);
Print "main: / n"; foreach (@arr) {print "$ _ / n";
$ count = 0; @arr = & test (@arr);
Foreach (@arr) {print "$ _ / N";
Sub test {print "/ nsub: / n"; shift @_; $ _ [0] = "g"; $ _ [10] = "h"; push @_, "f"; @_;} Run results : Main: abcde
Sub: GCDE
HF