Perl-13-subroutine-2

Source: Internet
Author: User
Tags call by reference

1. arrays and hash variables in parameters:

When defining the parameter list, there are two ways to define an array or hash variable as the input parameter:

1. Add the Backslash "/" before the type name of the array or hash variable.

2. Place the name of the array or hash variable type parameter at the end of the parameter list;

The array or hash variable passed as the parameter will be copied to the scalar Variable list in the parameter list to generate a list of parameters with an uncertain number; in fact, the array or hash variable of the scalar variable and the Backslash "/" are the most common parameter types, because the hash variable or array type indicator can contain all the remaining parameters;

InProgramDuring compilation, the value entered into the built-in array @ _ is a reference variable. Therefore, the @ _ array is actually a reference array; copy the reference of each scalar variable to the array @ _ in perl5. If you use an array unit (for example, $ _ [0]) to modify the content in the array, the actual variable passed in to the parameter table is modified instead of the variable backup;

This method of passing parameters is calledCall by reference (The parameter table references the actual variables in the memory. There is also a call method relative to the call by referenceCall by valueThis call method is implemented by copying the actual parameter values to the lexical variable table, and then operating the parameters in the lexical variable table. At this time, the backup parameters are modified, you don't have to worry about changing it to the actual parameter. Calling by value means copying a parameter, which is not suitable for Big Data Objects;

Modifying the actual input parameters is not perfect, so it is best not to use this method. to modify the content of the @ _ array cell to modify the actual parameter value, the subroutine must be informed of the input parameters. Note that when you directly modify the value of the input parameterCodeVariables that can be modified by the subroutine must be used. If a direct number is used in the call, and the subroutine needs to modify the actual input parameters, the program crashes due to a running error =; pay attention to this when calling by reference;

In addition, you can use aliases to pass array parameters. When you call an array in the form of & my_sub (@ array, perl copies all elements in the array @ array to the @ _ array of the subprogram. When the array @ array is large, this call method consumes resources and time, low efficiency; usableAlias TransferArray to avoid this copy operation, in order to directly operate on the array in the memory; this method is similar to passing the array address in the C language, but different, after defining the array alias, if a simple variable with the same name exists, the variable takes effect;

2. Access to the parameter list:
There are two methods to access the parameter list in Perl:

1. Use the subscript of the @ _ array to access each parameter. For example, $ _ [0] indicates the first parameter of the current function;

2. Use shift () and POP () functions to access the @ _ array of the current function;

Note: The variables in the subroutine are global variables by default. You can use the keyword "my" or "local" to change them to local variables;

The subprograms in Perl have no type. All subprograms use a list as a parameter. subprograms can be returned in a list or in a scalar. The definition of Perl subprograms cannot be nested, they can be defined anywhere, unless they are in the same block;

Iii. predefined subroutines provided by Perl:

Perl provides three predefined subprograms that are executed at specific times:

◆ Begin: called when the program is started;

◆ End: called at the end of the program;

◆ Autoload: called when a subprogram cannot be found;

Programmers can redefine these three subprograms to complete the specified operation at a specific time. If the same predefined subprogram defines multiple implementations, It will be executed in sequence, the end is executed in reverse order;

Iii. return values of subprograms:

The result produced by calling a subroutine is called the return value. Perl uses the return value to return the value to the main function;

By default, Perl returns the calculation result of the last line of code executed before the completion of the subroutine as the return value to the main function. In the statement that calls the subroutine, the variable context (scalar context or list context) on the left of the equal sign determines the context of the return value of the subroutine;

In addition to the default situation, Perl can also use the keyword return to return the return value of the subroutine. The return statement can return the execution result of the subroutine to the calling program and exit the execution of the subroutine, that is: the Return Statement explicitly specifies the value that the Code returns to the main program and where the subroutine stops;

The basic syntax of the return statement is as follows:

Return (scalar/list );

The Return Statement exits the execution of the subroutine and returns to the calling program. The list contains the returned values. Note: All arrays in the list are expanded into scalar values. If you want to return an array or hash variable, it is best to return an array or a reference to a hash variable;

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.