Perl function parameter Receiving Method

Source: Internet
Author: User

Restore Perl

Http://www.linuxsir.org/bbs/showthread.php? T = 78216

Perl
Function and scope.

Syntax:

Sub subroutinename

{

My
(@ Argstosubroutine) = @ _; # The parameter is passed to the function in this way.

& Dostuff ;#
Execution body

Return (@ returnvalue );#

}

Special variable @_
Is the parameter stack, Which is local data for the called subroutine. Every time a subroutine is called, the parameter is placed in. @ _ Works like C and C ++
Stack. Because the parameter stack is an array, there is no limit on the number of parameters passed to the function. No parameter is a scalar. When a subroutine is called, the length of the parameter stack is irrelevant. No matter what
The sub-programs that pass parameters to the reader. Perl generates @ _ and determines whether to receive parameters.

Operation Parameter Stack:

(In Perl
).

@ _ Can be accessed by subscript like other types of arrays.

You can also use Shift and pop
Function to access @ _. In fact, shift and pop indicate shift (@ _) Pop (@_)
It is two functions that operate on the parameter stack. Indicates whether to operate the parameter Stack from the top or bottom of the stack. Each time they are called, they take the first parameter (shift) or the last parameter (POP ). Then
In the variable that stores the parameter in the left side, shorten the value of @ _ when the execution continues @_.

Local @_
Stack. This feature allows you to call a function within a function, so you do not have to worry about the impact of this function on lower-level functions. This flexibility allows Perl to use recursive Programs,

The returned value stack is Perl.
Return the value to the caller of the subroutine. @ Values = subroutine ($ arguments ).
Same as the parameter stack, the return value stack is also an array.

The subfunction returns the value to the main function in two ways:

Use special function return methods

Use the default method
(View the last expression of the subroutine ).

1. Return keyword

Return
The keyword allows you to immediately interrupt the subfunction and return the value inside the parameter stack to the calling function.

2. Default return

If no return exists in the function
Statement, the last sentence of the subroutine is actually the return value stack.

3. wantarray

This function is Perl.
Sub-functions can execute functions with double responsibilities. Wantarray can determine whether a function is used in an environment that requires an array or a scalar environment.

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.