In Perl, function calls, terms, and statements have inconsistent explications which rely upon its Context. There are two crucial Contexts in Perl, namely List Context and Scalar Context. In a list context, Perl gives the list of elements. But in a scalar context, it returns the number of elements in the array.
Perl presumes List values in “List Context”, where as the list could have any number of elements or it can have only one element or it can even be desolated.
List Context can be generated with the use of Arrays and Lists.
- Assignment to an array:
@y = LIST;
@y = @z;
@y = localtime();
Here, localtime() is a function name in Perl, which unveils number depiction of time in array.
- Assignment to a list:
($x, $y) = LIST;
($x) = LIST;
Here, List can moreover create List Context even if the List has only one element.
geeks for geeks for geeks articles 201761121191690
Here, in assignment to List there are two scalars in the List i.e, $x and $y, so only two elements of the array are assigned.
In order to provoke List Context using an array, we need to assign an array to another array.
Here, elements from one array are copied to another array.
if-statement is used in a List context to display the statements enclosed within ‘if’ only when there are elements present in the array.
Here, if the stated array has some number of elements then if-condition is true and it provokes the content of the if-statement but if the array is empty then the if-condition is false so it does not execute the statements within if-statement.
“STDIN” is a readline operator in Perl. In order to place readline operator in List Context it is required to designate this operator to an array.
Here is how this program works:
Step 1: Enter the names which are to be stored in the array one by one with the use of enter key.
Step 2: Press Ctrl-D in Linux systems whereas Ctrl-Z in Windows system to indicate the ending of input.
Step 3: chomp is used to remove the extra line added after each input.
Step 4: Printing the number of elements in the array with the use of scalar because “an array in Scalar Context” can only return the length of an array.