Perl | List Context Sensitivity
Introduction
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:
Example:@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:
Example:($x, $y) = LIST;
($x) = LIST;
Here, List can moreover create List Context even if the List has only one element.
Example:
#!/usr/bin/perl# Perl program of creating List Context # array of elementsmy @CS = ('geeks', 'for', 'geeks', 'articles'); # Assignment to a list my ($x, $y) = @CS; # Assignment to an Arraymy @z = @CS; # Assignment of a function# to an Arraymy @t = localtime(); # Displays two elements of an# array in Listprint "$x $y\n"; # Displays an array of elementsprint "@z\n"; # Displays time stored in array # in number formatprint @t; |
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.
Example:
#!/usr/bin/perl # Program for arrays in List Contextuse strict;use warnings;use 5.010; my @x = ('computer_', 'science_', 'portal_', 'for_', 'GeeksforGeeks'); # Assignment of an array to # another arraymy @y = @x; # Printing the new arrayprint @y; |
computer_science_portal_for_GeeksforGeeks
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.
Example:
#!/usr/bin/perl # Program to display content of if-statementuse strict;use warnings;use 5.010; my @x = ('G', 'f', 'G'); # Statement within 'if' will be executed # only if the array is not emptyif (@x){ print "GeeksforGeeks";} |
GeeksforGeeks
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.
Example:
#!/usr/bin/perluse strict;use 5.010; # Asking user to provide input print "Enter the list of names:\n"; # Getting input from user my @y = <STDIN>; # Used to remove extra line of spaceschomp @y; # Printing the required outputprint "The number of names are: " . scalar(@y) . "\n"; |

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.

