Perl subroutines are also user-defined functions.
A Perl subroutine is a separated piece of code that performs a specific task, which can reduce repetitive code and make the program easier to read.
Perl subroutines can appear anywhere in the program, and the syntax format is as follows:
sub subroutine{ statements;}
Syntax format for calling subroutine:
subroutine(parameter list);
In versions below Perl 5.0, the method of calling a subroutine is as follows:
&subroutine(parameter list);
In the new version, although this calling method is also supported, its use is not recommended.
Next let's look at a simple example:
Executing the above program, the output result is:
Hello, World!
Perl subprograms can accept multiple parameters like other programming. Subprogram parameters are marked with the special array @_.
Therefore, the first parameter of the subroutine is $_[0], the second parameter is $_[1], and so on.
Regardless of whether the parameters are scalar or array, when the user passes the parameters to the subroutine, Perl calls them by reference by default.
Executing the above program, the output result is:
The parameters passed in are: 10 20 30 The value of the first parameter is: 10 The average value of the parameters passed in is: 20
Users can change the value of the corresponding actual parameter by changing the value in the @_ array.
Since the @_ variable is an array, it can be passed a list into the subroutine.
But if we need to pass in scalar and array parameters, we need to put the list on the last parameter, as shown below:
The above program combines scalars and arrays, and the output is:
The list is: 10 1 2 3 4
We can pass multiple arrays and hashes to the subroutine, but when passing in multiple arrays and hashes, independent identities will be lost. So we need to use references (will be introduced in the next chapter) to pass.
When a hash table is passed to the subroutine, it is copied into @_ and the hash table is expanded into a list of key/value combinations.
The output result of the execution of the above program is:
age: 3name: codercto
Subroutines can return function values using the return statement just like in other programming languages.
If no return statement is used, the last line of the subroutine is used as the return value.
The output result of the execution of the above program is:
3
We can return scalars, arrays and hashes in subroutines, but when returning multiple arrays and hashes, independent identities will be lost. So we need to use references (introduced in the next chapter) to return multiple arrays and functions.
By default, all variables in Perl are global variables, which means that the variables can be called anywhere in the program.
If we need to set a private variable, we can use the my operator to set it.
The my operator is used to create lexically scoped variables. The variables created through my survive from the beginning of the declaration until the end of the closed scope.
The closed scope refers to the area within a pair of curly braces, a file, or an if, while, for, foreach, or eval string.
The following example demonstrates how to declare one or more private variables:
sub somefunc { my $variable; # $variable is not visible outside the method somefunc() my ($another, @an_array, %a_hash); # Declare multiple variables at the same time}
The output result of the execution of the above program is:
String inside the function: Hello, Codercto! String outside the function: Hello, World!
We can use local to provide temporary values for global variables and return the original values after exiting the scope.
Variables defined by local do not exist in the main program, but exist in the subprogram and the subprograms called by the subprogram. You can assign a value to it when defining it, such as:
The output result of the execution of the above program is:
String value within the PrintMe function: Hello, Codercto!PrintCodercto String value within the function: Hello, Codercto!PrintHello String value within the function: Hello, World! String value outside the function: Hello, World!
The function of the state operator is similar to the static modifier in C. The state keyword makes local variables persistent.
state is also a lexical variable, so it is only valid in the lexical scope in which the variable is defined. For example:
The output result of the execution of the above program is:
The value of counter is: 0 counter The value is: 1 counter The value is: 2 counter The value is: 3 counter The value is: 4
Note 1: state can only create variables whose closed scope is within the subroutine.
Note 2: state was introduced from Perl 5.9.4, so use must be added before use.
Note 3: state can declare scalars, arrays, and hashes. But arrays and hashes cannot be initialized when they are declared (at least not in Perl 5.14).
During the subroutine call process, different types of values will be returned according to the context. For example, the following localtime() subroutine returns a string in a scalar context and a list in a list context:
The output result of the execution of the above program is:
Sun Jun 12 15:58:09 20162106-6-12 15:58:9