Subroutines in Perl learn notes _perl

Source: Internet
Author: User
Tags autoload aliases scalar

I. Definition

Functions can be divided into system functions and user functions. A subroutine is a separate piece of code that performs a special task, which makes it easy to reduce duplicate code and make the program readable. In Perl, subroutines can appear anywhere in the program. The method is defined as:

Copy Code code as follows:

Sub subroutine{
statements;
}

Second, call
The method is invoked as follows:
1. Use & Call
Copy Code code as follows:

&subname;
...
Sub subname{
...
}

2, first defined after the call, you can omit the & symbol
Copy Code code as follows:

Sub subname{
...
}
...
SubName;

Suggested that this approach be taken.
3, forward reference, first the subroutine name, followed by the definition of subroutine body
Copy Code code as follows:

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 returns a value Retval,retval can be a list.
Four, local variables
There are two ways to define a local variable in a subroutine: my and locals. The difference is that a variable of my definition exists only in that subroutine, while the local-defined variable does not exist in the main program, but it exists in the subroutine and in the subroutine called by the subroutine (no my in PERL4). You can assign a value to it when you define it, such as:
Copy Code code as follows:

My ($scalar) = 43;
Local (@array) = (1, 2, 3);

sub-Program parameter transfer
1. Form
Copy Code code as follows:

&sub1 (&number1, $number 2, $nubmer 3);
...
Sub sub1{
My ($number 1, $number 2, $number 3) = @_;
...
}

2. Transmit Array
Copy Code code as follows:

&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 variable. Such as
Copy Code code as follows:

Sub Twolists {
My (@list1, @list2) = @_;
}

The @list2 must be empty. But simple variables and array variables can be passed at the same time:
Copy Code code as follows:

&twoargs (@mylist); # 47 assigned to $scalar, @mylist assigned to @list
&twoargs (@mylist); # The first element of @mylist is assigned to $scalar, and the rest of the elements are assigned to @list
...
Sub Twoargs {
My ($scalar, @list) = @_;
...
}

Six, Recursive subroutine

In Perl, subroutines can be invoked with each other, and the calling method is the same as the one described above, which becomes a recursive subroutine when the subroutine itself is invoked. Recursive subroutine has two conditions: 1, in addition to not quilt changes in the program, all the variables must be local; 2, the subroutine contains code to stop the call itself.

Vii. passing array parameters with aliases

1, with the previous call method &my_sub (@array) to copy the array @array data to the subroutine in the variable @_, when the array is very large, will spend more resources and time, and the use of Alias Pass will not do these work, and the array direct operation. Forms such as:

Copy Code code as follows:

@myarray = (1, 2, 3, 4, 5);
&my_sub (*myarray);
Sub My_sub {
My (*subarray) = @_;
}

2, this method is similar to the C language in the transfer array of the starting address pointer, but not the same, after defining an array alias, if there is a simple variable with the same name, it also works on the variable. Such as:
Copy Code code as follows:

$foo = 26;
@foo = ("Here ' s", "a", "list");
&testsub (*foo);
...
Sub TestSub {
Local (*printarray) = @_;
...
$printarray = 61;
}

When the subroutine is finished, the value of the $foo in the main program is already 61, not 26.
3, the method of using aliases can pass multiple arrays, such as:
Copy Code code as follows:

@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 array1 alias, and Subarray2 is the Array2 alias.
Eight, predefined subroutines
PERL5 three subroutines, which are executed at specific times, when the Begin subroutine is invoked at the start of the program, and the end subroutine is called when the program ends, and the AutoLoad subroutine is invoked when a subroutine is not found. You can define them yourself to perform the required actions at specific times. Such as:
Copy Code code as follows:

BEGIN {
Print ("hi! Welcome to perl!\n ");
}
autoload{
Print ("subroutine $AUTOLOAD not found\n"); # variable $autoload is the name of the subroutine that is not found
Print ("Arguments passed: @_\n");
}

If more than one predefined subroutine is defined, the begin sequence executes, and the end is executed in reverse order.

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.