I. Definition
Child Program That is, to execute a separate section of a special task Code It can reduce repeated code and make the program readable. In Perl, subroutines can appear anywhere in the program. Definition method:
Sub subroutine {
Statements;
}
Ii. Call
The call method is as follows:
1. Use and call
& Subname;
...
Sub subname {
...
}
2. Define and call it first. The & symbol can be omitted.
Sub subname {
...
}
...
Subname;
3. Forward reference: first define the subprogram name, and then define the subprogram body.
Sub subname;
...
Subname;
...
Sub subname {
...
}
4. Use do to call
Do my_sub (1, 2, 3); equivalent to & my_sub (1, 2, 3 );
Iii. return values
By default, the value of the last statement in the subroutine is used as the return value. Statement return (retval); you can also launch a subroutine and return a retval value. retval can be a list.
Iv. Local Variables
There are two methods to define local variables in a subroutine: My and local. The difference is that the variables defined by my only exist in this subroutine, while those defined by local do not exist in the main program, but it exists in the subprogram and the subprogram called by the subprogram (no my In perl4 ). You can assign a value to a definition, for example:
My ($ scalar) = 43;
Local (@ array) = (1, 2, 3 );
V. subroutine parameter transfer
1. Form
& Sub1 (& number1, $ number2, $ nubmer3 );
...
Sub sub1 {
My ($ number1, $ number2, $ Number3) = @_;
...
}
2. Transfer An Array
& Addlist (@ mylist );
& Addlist ("14", "6", "11 ");
& Addlist ($ value1, @ sublist, $ value2 );
...
Sub addlist {
My (@ list) = @_;
...
}
When the parameter is an array, the subroutine only assigns it to an array variable. For example
Sub twolists {
My (@ list1, @ list2) = @_;
}
@ List2 must be empty. However, simple variables and array variables can be passed simultaneously:
& Twoargs (47, @ mylist); #47 assigns $ scalar and @ mylist to @ list
& Twoargs (@ mylist); # @ the first element of mylist is assigned to $ scalar, and other elements are assigned to @ list
...
Sub twoargs {
My ($ scalar, @ list) = @_;
...
}
6. Recursive subprograms
In Perl, subprograms can call each other. The Calling method is the same as above. When the subprogram itself is called, it becomes a recursive subprogram. Recursive subprograms have two conditions: 1. Except for variables not changed by the subprogram, all variables must be local; 2. the subprogram must contain the code to stop calling itself.
7. Passing array parameters with aliases
1. Use the call Method & my_sub (@ array) mentioned above to copy the data of array @ array to the variable @ _ In the subroutine. When the array is large, it will spend a lot of resources and time, and pass the alias will not do this work, but directly operate on the array. The format is as follows:
@ Myarray = (1, 2, 3, 4, 5 );
& My_sub (* myarray );
Sub my_sub {
My (* subarray) = @_;
}
2. This method is similar to passing the starting address pointer of an array in C language, but it is not the same. After defining the alias of an array, if there is a simple variable with the same name, this variable also works. For example:
$ Foo = 26;
@ Foo = ("here's", "A", "list ");
& Testsub (* Foo );
...
Sub testsub {
Local (* printarray) = @_;
...
$ Printarray = 61;
}
After the subroutine is executed, the value of $ Foo in the main program is 61, instead of 26.
3. the alias method can be used to pass multiple arrays, for example:
@ 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 the alias of array1 and subarray2 is the alias of array2.
8. predefined subprograms
Perl5 predefines three subprograms, which are executed at specific time: the in subprogram is called at program startup; the end subprogram is called at program end; the autoload subroutine is called when a subroutine cannot be found. You can define them by yourself to perform the required actions at a specific time. For example:
Begin {
Print ("Hi! Welcome to Perl! \ N ");
}
Autoload {
Print ("subroutine $ autoload not found \ n"); # variable $ autoload is the name of the unfound subroutine
Print ("arguments passed: @ _ \ n ");
}
If multiple predefined subprograms are defined in the same predefined subprograms, The begin statements are executed sequentially and the end statements are executed in reverse order.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.