1. Definition
A subroutine is a separate piece of code that executes a special task. It can reduce duplicate code and make the program readable. In PERL, subroutines can appear anywhere in the program. The definition method is:
sub subroutine{
statements;
}
2. Call
The call method is as follows:
1. Use & call
&subname;
...
sub subname{
...
}
2. Define first and then call, and the & symbol can be omitted
sub subname{
...
}
...
subname;
3. Forward reference, first define the subroutine name, then define the subroutine body
sub subname;
...
subname;
...
sub subname{
...
}
4. Call with do
do my_sub(1, 2, 3); is equivalent to &my_sub(1, 2, 3);
3. Return value
By default, the value of the last statement in the subroutine will be used as the return value. The statement return (retval); can also deduce a subroutine and return the value retval, which can be a list.
4. Local variables
There are two ways to define local variables in a subroutine: my and local. The difference is that the variables defined by my only exist in the subroutine; while the variables defined by local do not exist in the main program, but exist in the subroutine and the subroutine called (there is no my in PERL4). It can be assigned a value when defining, such as:
my($scalar) = 43;
local(@array) = (1, 2, 3);
5. Subprogram parameter transfer
1. Form
&sub1(&number1, $number2, $nubmer3);
...
sub sub1{
my($number1, $number2, $number3) = @_;
...
}
2. Transfer array
&addlist (@mylist);
&addlist ("14", "6", "11");
&addlist ($value1, @sublist, $value2);
...
sub addlist {
my (@list) = @_;
...
}
When the parameter is an array, the subroutine assigns it to only one array variable. like
sub twolists {
my (@list1, @list2) = @_;
}
@list2 must be empty. But simple variables and array variables can be passed at the same time:
&twoargs(47, @mylist); #47 assigns to $scalar, @mylist assigns to @list
&twoargs(@mylist); # The first element of @mylist is assigned to $scalar, and the remaining elements are assigned to @list
...
sub twoargs {
my ($scalar, @list) = @_;
...
}
6. Recursive subroutine
In PERL, subroutines can be called to each other, and their calling method is the same as above. When the subroutine itself is called, it becomes a recursive subroutine. There are two conditions for recursive subroutines: 1. Except for variables that are not changed by the subroutine, all variables must be local; 2. The subroutine must contain code to stop the call itself.
7. Pass array parameters with alias
1. Use the call method &my_sub(@array) mentioned above to copy the data of the array @array to the variable @_ in the subprogram. When the array is large, it will take more resources and time, and passing with alias will not do these tasks, but will directly operate on the array. Form:
@myarray = (1, 2, 3, 4, 5);
&my_sub(*myarray);
sub my_sub {
my (*subarray) = @_;
}
2. This method is similar to the starting address pointer of the passing array in C language, but it is not the same. After defining the alias of the array, if there is a simple variable with the same name, it will also work on the variable. like:
$foo = 26;
@foo = ("here's", "a", "list");
&testsub (*foo);
...
sub testsub {
local (*printarray) = @_;
...
$printarray = 61;
}
When the subroutine is executed, the value of $foo in the main program has become 61, not 26 anymore.
3. Multiple arrays can be passed by alias, such as:
@array1 = (1, 2, 3);
@array2 = (4, 5, 6);
&two_array_sub (*array1, *array2);
sub two_array_sub {
my (*subarray1, *subarray2) = @_;
}
In this subroutine, subarray1 is an alias for array1 and subarray2 is an alias for array2.
8. Predefined subroutines
PERL5 predefined three subroutines, which are executed at a specific time. They are: the BEGIN subroutine is called when the program starts; the END subroutine is called when the program ends; and the AUTOLOAD subroutine is called when the subroutine cannot be found. You can define them yourself to perform the required actions at a specific time. like:
BEGIN {
print("Hi! Welcome to Perl!\n");
}
AUTOLOAD{
print("subroutine $AUTOLOAD not found\n"); # Variable $AUTOLOAD is the subroutine name not found
print("arguments passed: @_\n");
}
If multiple definitions are defined by the same predefined subroutine, then BEGIN is executed sequentially and END is executed in reverse order.
A subroutine is a separate piece of code that executes a special task. It can reduce duplicate code and make the program readable. In PERL, subroutines can appear anywhere in the program. The definition method is:
sub subroutine{
statements;
}
2. Call
The call method is as follows:
1. Use & call
&subname;
...
sub subname{
...
}
2. Define first and then call, and the & symbol can be omitted
sub subname{
...
}
...
subname;
3. Forward reference, first define the subroutine name, then define the subroutine body
sub subname;
...
subname;
...
sub subname{
...
}
4. Call with do
do my_sub(1, 2, 3); is equivalent to &my_sub(1, 2, 3);
3. Return value
By default, the value of the last statement in the subroutine will be used as the return value. The statement return (retval); can also deduce a subroutine and return the value retval, which can be a list.
4. Local variables
There are two ways to define local variables in a subroutine: my and local. The difference is that the variables defined by my only exist in the subroutine; while the variables defined by local do not exist in the main program, but exist in the subroutine and the subroutine called (there is no my in PERL4). It can be assigned a value when defining, such as:
my($scalar) = 43;
local(@array) = (1, 2, 3);
5. Subprogram parameter transfer
1. Form
&sub1(&number1, $number2, $nubmer3);
...
sub sub1{
my($number1, $number2, $number3) = @_;
...
}
2. Transfer array
&addlist (@mylist);
&addlist ("14", "6", "11");
&addlist ($value1, @sublist, $value2);
...
sub addlist {
my (@list) = @_;
...
}
When the parameter is an array, the subroutine assigns it to only one array variable. like
sub twolists {
my (@list1, @list2) = @_;
}
@list2 must be empty. But simple variables and array variables can be passed at the same time:
&twoargs(47, @mylist); #47 assigns to $scalar, @mylist assigns to @list
&twoargs(@mylist); # The first element of @mylist is assigned to $scalar, and the remaining elements are assigned to @list
...
sub twoargs {
my ($scalar, @list) = @_;
...
}
6. Recursive subroutine
In PERL, subroutines can be called to each other, and their calling method is the same as above. When the subroutine itself is called, it becomes a recursive subroutine. There are two conditions for recursive subroutines: 1. Except for variables that are not changed by the subroutine, all variables must be local; 2. The subroutine must contain code to stop the call itself.
7. Pass array parameters with alias
1. Use the call method &my_sub(@array) mentioned above to copy the data of the array @array to the variable @_ in the subprogram. When the array is large, it will take more resources and time, and passing with alias will not do these tasks, but will directly operate on the array. Form:
@myarray = (1, 2, 3, 4, 5);
&my_sub(*myarray);
sub my_sub {
my (*subarray) = @_;
}
2. This method is similar to the starting address pointer of the passing array in C language, but it is not the same. After defining the alias of the array, if there is a simple variable with the same name, it will also work on the variable. like:
$foo = 26;
@foo = ("here's", "a", "list");
&testsub (*foo);
...
sub testsub {
local (*printarray) = @_;
...
$printarray = 61;
}
When the subroutine is executed, the value of $foo in the main program has become 61, not 26 anymore.
3. Multiple arrays can be passed by alias, such as:
@array1 = (1, 2, 3);
@array2 = (4, 5, 6);
&two_array_sub (*array1, *array2);
sub two_array_sub {
my (*subarray1, *subarray2) = @_;
}
In this subroutine, subarray1 is an alias for array1 and subarray2 is an alias for array2.
8. Predefined subroutines
PERL5 predefined three subroutines, which are executed at a specific time. They are: the BEGIN subroutine is called when the program starts; the END subroutine is called when the program ends; and the AUTOLOAD subroutine is called when the subroutine cannot be found. You can define them yourself to perform the required actions at a specific time. like:
BEGIN {
print("Hi! Welcome to Perl!\n");
}
AUTOLOAD{
print("subroutine $AUTOLOAD not found\n"); # Variable $AUTOLOAD is the subroutine name not found
print("arguments passed: @_\n");
}
If multiple definitions are defined by the same predefined subroutine, then BEGIN is executed sequentially and END is executed in reverse order.