Perl is context-related and different from many other languages. The interpretation of values in Perl sometimes depends on the context of the operation or value. The context includes numbers, strings, lists, and flags. There are two common contexts:Scalar contextAndList Context.
The return value operation requires a list, and the scalar value is the same. That is to say, Perl reloads certain operations based on whether the expected return value is singular or plural. In a scalar context, the expression is returned with a scalar value. In a list context, the expression is returned with a list of scalar values. This is the difference between the two.
The scalar context affects whether data is calculated as numbers or strings. The list context affects the data returned by the value assignment statement and the parameters and return values of subprograms or system functions;
Correspondingly, the function provides a scalar context and a list context for each parameter.
Example 1: int (<stdin> );
This function provides a scalar context for its parameters. Because this function is to convert its parameters into an integer, only one scalar parameter is required. Therefore, <stdin> the operation reads only one scalar data and passes it to int () as a parameter. If multiple data entries are input, only the first data is transmitted to the INT () function (), return the converted integer scalar value.
Example 2: Sort (<stdin> );
This function sorts the elements in a list and requires a list as a parameter. Therefore, this function provides a list context for <stdin> operations. Therefore, the Perl interpreter packs all input elements produced by the <stdin> operation as list elements into a list, and then passes the list as a parameter to the sort () function for sorting, after the sort () function completes sorting, it packs all the sorted data elements into a list and returns the sorted list as the return value to sort () the context of the function, that is, the context of the list.
The value assignment operator "=" is a bit special. It uses the parameter on the left of the equal sign to determine the context of the parameter on the right of the equal sign. Assign a value to a scalar variable, which provides a scalar context for the parameter value on the right of the equal sign; assign a value to an array or an array segment, provides a list context for the parameter values on the right of the equal sign;
User-Defined subprograms may be concerned about whether they are called by a scalar context or a list context, but most subprograms do not need to care about them, because scalar variables are automatically inserted into a list;
A list is a series of projects in a data container. in Perl, a list refers to a series of scalar values. You can use parentheses or arrays to generate a list context. The list can also contain another list. The Perl interpreter first converts all the child lists in the parent list into a series of scalar variables, and finally lists all scalar variables in the parent list, includes scalar variables in the sublist. This is important when using subroutines. When using a list containing sub-lists, the perl interpreter first lists the scalar variables in all sub-lists before processing the entire list.
Example:
@ Name = (liumin, wangya, Tongtong, Liuying );
@ Sex = ('male', 'female ');
@ Age = (24, 26 );
@ Friends = (@ name, @ sex, @ age );
In the preceding statement, before assigning values to the list @ friends, the perl interpreter first parses its sublists @ name, @ sex, and @ age into the corresponding scalar list values, in this way, the content of the list (@ name, @ sex, @ age) is changed:
(Liumin, wangya, Tongtong, Liuying, male, female, 24, 26)
And then assign the new list to the list @ friends. Finally, the form of the List @ friends is as follows:
@ Friends = (liumin, wangya, Tongtong, Liuying, 'male', 'female ', 24, 26 );
The Perl interpreter parses the sublist from left to right. This principle is also followed when the list is used for subprograms or function calls;