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.