The sub-program of Perl Learning

Source: Internet
Author: User
Tags autoload aliases define local

First, the definition
A subroutine is a separate piece of code that performs a special task, which can reduce duplication of code and make the program easier to read. In Perl, subroutines can appear anywhere in the program. The definition method is:
Sub subroutine{
statements;
}
Second, call
The calling method is as follows:
1. Call with &
&subname;
...
Sub subname{
...
}
2, the first definition after the call, you can omit the & symbol
Sub subname{
...
}
...
SubName;
3, forward reference, first set the subroutine name, and then define the sub-program body
Sub SubName;
...
SubName;
...
Sub subname{
...
}
4. Call with Do
Do My_sub (1, 2, 3); equivalent to &my_sub (1, 2, 3);
Third, return value
By default, the value of the last statement in the subroutine is used as the return value. Statement return (retval), or you can eject the subroutine and return the value Retval,retval can be a list.
Four, local variables
There are two ways to define local variables in a subroutine: my and Local. The difference is that the variable I defines is only present in the subroutine, and the local defined variable does not exist in the main program, but exists in the subroutine and in the subroutine called by the subroutine (without my in PERL4). You can assign a value to it when you define it, such as:
My ($scalar) = 43;
Local (@array) = (1, 2, 3);
Five, sub-program parameter transfer
1. Form
&sub1 (&number1, 2, $nubmer 3);
...
Sub sub1{
My (1, 2, 3) = @_;
...
}
2. Transferring arrays
&addlist (@mylist);
&addlist ("14", "6", "11");
&addlist ($value 1, @sublist, $value 2);
...
Sub Addlist {
My (@list) = @_;
...
}
When a parameter is an array, the subroutine assigns it to only one set of variables. Such as
Sub Twolists {
My (@list1, @list2) = @_;
}
The @list2 must be empty. But simple variables and array variables can be passed at the same time:
&twoargs (@mylist); # 47 to $scalar, @mylist to @list
&twoargs (@mylist); # The first element of the @mylist is assigned to $scalar, and the remaining elements are assigned to @list
...
Sub Twoargs {
My ($scalar, @list) = @_;
...
}
Vi. Recursive sub-procedures
In Perl, subroutines can be called to each other, and the method of invocation is the same as above, and when the subroutine itself is called, it becomes a recursive subroutine. The recursive subroutine has two conditions: 1, except for the variable that does not change the quilt, all variables must be local; 2, the subroutine should contain the code to stop the call itself.
Vii. passing array parameters with aliases
1, the Call Method &my_sub (@array) will copy the data of the array @array to the variable @_ in the subroutine, when the array is large, it will spend more resources and time, and the alias Pass will not do the work, but the array directly operation. Forms such as:
@myarray = (1, 2, 3, 4, 5);
&my_sub (*myarray);
Sub My_sub {
My (*subarray) = @_;
}
2. This method is similar to the start address pointer of the passed array in C, but not the same, if you have a simple variable of the same name after the alias of the array is defined, it also works for that variable. Such as:
$foo = 26;
@foo = ("Here ' s", "a", "list");
&testsub (*foo);
...
Sub TestSub {
Local (*printarray) = @_;
...
$printarray = 61;
}
When the subroutine finishes executing, the value of the $foo in the main program is 61, not 26.
3. Aliases can be used to pass multiple arrays, 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 the alias of Array1, and Subarray2 is the alias of Array2.
VIII. Pre-defined sub-procedures
PERL5 pre-defined three subroutines, which are executed at a specific time: The Begin subroutine is called at the start of the program, the end subroutine is called at the end of the program, and the AutoLoad subroutine is called when a subroutine is not found. You can define them yourself to perform the required actions at a specific time. Such as:
BEGIN {
Print ("hi! Welcome to perl!\n ");
}
autoload{
Print ("subroutine $AUTOLOAD not found\n"); # variable $autoload is the name of the subroutine that was not found
Print ("Arguments passed: @_\n");
}
If more than one predefined subroutine is defined, the begin sequence executes and end is executed in reverse order.

The sub-program of Perl Learning

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.