Perl | Scalar Context Sensitivity
Last Updated :
29 Mar, 2019
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. When an operator functions on Scalars then its termed as Scalar Context.
Note:
- 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.
Creating a Scalar Context
Scalar Context can be generated with the use of Scalar variables, Numerical operator, and many more.
- Assignment to a Scalar variable:
Example:
$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:
Example:
$a[2] = Scalar;
Every element of an array is individually a Scalar. So, assignment to them generates Scalar Context.
- Numerical operators creating Scalar Context:
Example:
3 + Scalar;
Scalar + 3;
A numerical operator can generate Scalar Context on either sides of it.
- Concatenation creating Scalar Context:
Example:
"GFG" . Scalar;
Scalar . "GFG"
From the above example, it is clear that Concatenation can generate Scalar Context on both side of itself.
Example:
my @CS = ( 'geeks' , 'for' , 'geeks' , 'articles' );
my $x = @CS ;
my $y = localtime ();
my $z = 3 + @CS ;
print "$x\n" ;
print "$y\n" ;
print "$z\n" ;
print "The number of elements are: " . @CS
|
Output:
4
Wed Mar 27 07:01:56 2019
7
The number of elements are: 4
Forcing Scalar Context
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.
Example:
my @x = ( 'geeks' , 'for' , 'geeks' );
print scalar @x ;
print "\n" ;
print scalar localtime ();
|
Output:
3
Sun Mar 17 06:12:53 2019
Arrays in Scalar Context
In order to provoke Scalar Context using an array, it is required to assign an array to a Scalar variable.
Example:
my @x = ( 'geeks' , 'for' , 'geeks' );
my $y = @x ;
print $y ;
|
Use of if-statement in Scalar Context
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.
Program 1:
use strict;
use warnings;
use 5.010;
my @w = ();
if ( @w )
{
print "Geeks" ;
}
|
Here, nothing is printed as the stated Array is empty. So, the code does not displays the content of the if-statement.
Program 2:
use strict;
use warnings;
use 5.010;
my @w = ( 'G' , 'f' , 'G' );
if ( @w )
{
print "There are some elements in the Array" ;
}
|
Output:
There are some elements in the Array
Here, the above stated Array is not empty so, the content of the if-statement is printed.
Reading in SCALAR Context
In order to place readline operator (i.e, <STDIN>) in Scalar Context it is required to designate this operator to a scalar variable.
Example:
use strict;
use 5.010;
print "Enter your name:\n" ;
my $y = <STDIN>;
print "My name is $y\n" ;
|
Output:
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...