Perl-13-subroutine-1

Source: Internet
Author: User
Tags perl interpreter

I. subprogram concept:

A subroutine is a function. It is a piece of code that executes the separation of special tasks. Using a subroutine can improve the reusability of the Code. From the perspective of usage, subprograms can be divided into four types: system functions, user-defined functions, parameter functions, and no-parameter functions;

The Perl subroutine can appear anywhere in the program. The declaration of the subroutine uses the keyword sub to do it. The syntax for declaring a non-argument function and a parameter function is as follows:

Sub subprogram name;

Sub subprogram name (parameter list );

Only declared but not defined subprograms can be used in the list. Defining subprograms is to add a block of code after the declaration of subprograms. The definitions of non-parametric and parametric functions are as follows:

Sub subprogram name

{

# The body of this function

}

Sub subroutine name (parameter list)

{

# The body of this function

}

You can also define anonymous subprograms. The syntax is as follows:

$ Functionref = sub {# the body of this function };

When an anonymous subroutine is defined, the last Semicolon is required because it is a value assignment statement;

You can also define an empty subroutine. The syntax is as follows:

Sub subroutine name {}

Sub subroutine name (parameter list ){}

Empty subprograms do nothing and have no practical significance. It indicates that the functions of these subprograms are not clear for the time being and will be implemented or expanded after preferential treatment;

Ii. subroutine call:

In Perl, there are multiple calling syntaxes for subprograms. Different calling syntaxes have different influences on the parameters passed to subprograms. The array "@ _" is an important array in Perl, it is also a built-in array used to store the list of parameters received by the subroutine;

The calling methods of subprograms can be divided into direct and indirect calls. The method of calling subprograms by reference is called indirect call; the method of calling a subroutine without reference is called direct call;

In versions earlier than perl5 (excluding perl5), when calling a subroutine, the bitwise AND operator "&" must be added before the subroutine name. to tell the perl interpreter to let this subroutine use its own parameter list; perl5 can certainly continue to be used in this way, and the function is the same, but in a new reference function added by perl5, the syntax for calling a subroutine by using a subroutine reference is as follows:

& $ Funref (parameter list );

At this time, the bitwise AND operator will tell the perl interpreter to find the address referenced by the subroutine from the subroutine list; therefore, it is best not to use the bitwise AND operator or subroutine reference method to call the subroutine in perl5; it is difficult to understand the code reading with bitwise AND operators in earlier versions;

List of subroutine call methods:

1. subfunname;

Directly call and pass the global @ _ array of the block as the parameter list to the program to be tuned;

2. subfunname (parameter list );

Call directly to generate a local backup of the @ _ array and set it to the scalar value of the parameter table;

3. subfunname parameter list;

Directly call to generate a local backup of the @ _ array and set it to the scalar value of the parameter table. At this time, the called subroutine must be declared in advance.

Subprograms;

4. & subfunname;

Directly call and pass the global @ _ array of the block as a parameter table to the program to be tuned;

5. & subfunname (parameter list );

Call directly to generate a local backup of the @ _ array and set it to the scalar value of the parameter table;

6. & $ subfunref;

Indirect call: Call a subroutine by referencing it. bitwise AND operator & is required. Pass the global @ _ array of the block as the parameter list.

Program to be tuned;

7. & $ subfunref parameter list;

Indirect call: generate a local backup of the @ _ array. The array value is equal to the scalar value of the parameter table;

In perl5, the subprogram can be called directly, as long as the parameters passed when the subprogram is called comply with the parameter rules in the parameter list when the subprogram is defined; when a subroutine is called, if the passed parameter does not comply with the rule of the parameter in the parameter list provided by the subroutine during definition, the program will have a syntax error. In this case, to solve this problem, add the bitwise AND operator "&" before the subroutine name when calling the subroutine to tell the perl interpreter, use the actual parameter list that is passed to the subroutine or use the global @ _ array of the block as the actual parameter list to pass to the subroutine; this is the purpose of the bitwise AND operator "&". By adding the bitwise AND operator (&) before the subroutine name, you can call the subroutine in any expression;

Pay attention to the following points when calling a subroutine:

◆ If no parameter list is available and the bitwise AND operator & is used when the subroutine is called, The Global @ _ array backup of the block is passed to the subroutine;

◆ If the parameter list (real parameter list) is used when a subroutine is called, The @ _ array generates a local backup of the parameter list;

◆ If the bitwise AND operator & is used with the actual parameter list and the actual parameter list is placed in parentheses, the actual parameter list is passed to the called subroutine, and the data is backed up in the local @ _ array;

◆ If the actual parameter list is not in parentheses, The Global @ _ array Backup will be passed to the called subroutine;

◆ When implementing recursive subroutines, note: Except for variables not changed by the quilt program, all other variables must be local and contain recursive termination conditions for the end call subroutine itself;

Iii. List of subroutine parameters:

If a list enclosed by parentheses is followed by a function call, during the function call, this list will be automatically backed up by the perl interpreter and stored in the special built-in array variable @ _ of Perl. The function can access this special variable to determine the number of parameters and assign values;

In Perl, when a subroutine without the parameter list declaration and definition is called, the number and type of parameters passed to the subroutine are not limited, the subroutine with parameter list declaration and definition must be called according to the parameter rules provided at the Declaration and definition. Otherwise, a call error may occur.

When declaring or defining a function, the parameter list after the function name is called a formal parameter (for short, a form parameter). When calling a function, the parameter list after the function name is called an actual parameter (for short, a real parameter );

When declaring or defining a subroutine, the parameter list defines the number and type of parameters required by the subroutine. When defining the parameter list, you must place the type indicator of each parameter in parentheses. Each type indicator defines the type of the parameter required for this position.

In the parameter list, use semicolons (;) to separate mandatory and optional parameters. The parameters listed before the semicolon are mandatory and the parameters listed after the semicolon are optional;

In Perl, if there is a backslash "/" before declaring or defining a function parameter, it indicates that the reference variable in the built-in special array @ _ is to be written. The type of the input parameter must be the same as the type of the corresponding parameter given during the declaration or definition. That is, the form parameter with a backslash transmits a reference to the variable;

Parameter List sample:

1 ,():

Parameters are not required, that is, they can be of any type or number;

2. ($ ):

Only one parameter is required and must be a scalar parameter;

3. ($;/% ):

A required parameter (scalar) and an optional parameter (reference of the hash variable) are required. A required parameter is prior to the semicolon, and an optional parameter is followed by the semicolon, however, this optional parameter must be a reference to a hash variable. The result is that a mandatory parameter and a reference to a hash variable are saved to the array;

4. ($ @):

The first three parameters are required to be scalar parameters, and the remaining parameters are placed in the array type variables;

5. (// % $; $ ):

The first three parameters are required to be required, and the last two parameters are optional. The first parameter is an array reference, and the second parameter is a hash variable reference, the third parameter is the scalar variable in the context; the last two optional parameters are the scalar variable;

If no backslash is added before a declared or defined parameter, the context of the specified character of the mandatory Type of the parameter becomes a real parameter. For example, the parameter is a scalar type specified character, but the real parameter is an array, the scalar context is used for real parameters, that is, the length of the array is passed to the subroutine rather than the array value;

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.