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. When an operator functions on Scalars then its termed as Scalar Context.
- Whenever you assign anything to a Scalar variable it will always give Scalar Context.
- In this Context, presumption is to obtain a single value.
- An array if assigned to Scalar variable will return its size.
Scalar Context can be generated with the use of Scalar variables, Numerical operator, and many more.
- Assignment to a Scalar variable:
$x = @z;
$x = localtime();
$x = Scalar;
Here, localtime() displays time in human readable format whereas in List Context this function shows number depiction of time.
- Assignment to a single element of an array:
$a = Scalar;
Every element of an array is individually a Scalar. So, assignment to them generates Scalar Context.
- Numerical operators creating Scalar Context:
3 + Scalar;
Scalar + 3;
A numerical operator can generate Scalar Context on either sides of it.
- Concatenation creating Scalar Context:
"GFG" . Scalar;
Scalar . "GFG"
From the above example, it is clear that Concatenation can generate Scalar Context on both side of itself.
4 Wed Mar 27 07:01:56 2019 7 The number of elements are: 4
One must require to force Scalar Context when Perl presumes a List. So, in that case you can utilize scalar() function which generates Scalar Context as Perl is informed by this function to impart Scalar Context for its parameters.
3 Sun Mar 17 06:12:53 2019
In order to provoke Scalar Context using an array, it is required to assign an array to a Scalar variable.
When the condition section of the if-statement presumes a single value then that is Scalar Context. In the below program, if-statement contains array, in scalar context, array returns the number of elements in it. So, if the array is empty then it will return 0 hence, if-statement will not execute if the array passed to it as scalar context is empty.
Here, nothing is printed as the stated Array is empty. So, the code does not displays the content of the if-statement.
There are some elements in the Array
Here, the above stated Array is not empty so, the content of the if-statement is printed.
In order to place readline operator (i.e, <STDIN>) in Scalar Context it is required to designate this operator to a scalar variable.
Above program accepts the input from the user with the use of <STDIN> and store it in the Scalar variable. Further, use that scalar variable to print the Input provided by the user.
- Perl | List Context Sensitivity
- Perl | Hash in Scalar and List Context
- Perl | STDIN in Scalar and List Context
- Perl | scalar keyword
- jQuery | context Property
- Perl vs C/C++
- Perl vs Python
- Perl | int() function
- Perl | q operator
- Perl | ord() Function
- Packages in Perl
- Perl | References
- Perl | last in loop
- Perl | Variables
- Perl | lt operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.