Open In App

Perl | Scalar 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. When an operator functions on Scalars then its termed as Scalar Context.
Note:

Creating a Scalar Context

 
Scalar Context can be generated with the use of Scalar variables, Numerical operator, and many more.



Example:




#!/usr/bin/perl 
# Perl program of creating Scalar Context 
  
# array of elements 
my @CS = ('geeks', 'for', 'geeks', 'articles'); 
              
# Assignment to a Scalar variable
my $x = @CS;         
      
# Assignment of a function 
# to a Scalar variable 
# Note: Time displayed here
# will be the GMT
my $y = localtime(); 
  
# Numerical operator creating
# Scalar Context
my $z = 3 + @CS;
              
# Displays number of elements
# in an Array
print "$x\n";     
  
# Displays time stored in array 
# in human readable format
print "$y\n";
  
# Displays sum of a number
# and Scalar
print "$z\n";
  
# Concatenation creating 
# Scalar Context
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:




#!/usr/bin/perl 
# Perl program of Forcing Scalar Context 
  
# array of elements 
my @x = ('geeks', 'for', 'geeks'); 
  
# Forcing Scalar context to display
# number of elements in an Array
print scalar @x;
print "\n";
  
# Displaying time in human readable 
# format by forcing Scalar Context
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:




#!/usr/bin/perl 
# Perl program of Arrays in Scalar Context 
  
# array of elements 
my @x = ('geeks', 'for', 'geeks'); 
  
# Assignment of an Array to
# a Scalar variable
my $y @x;
  
# Displays number of elements in
# an Array
print $y;         

Output:
3

 

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:




#!/usr/bin/perl 
  
# Program of if-statement in Scalar Context 
use strict; 
use warnings; 
use 5.010; 
  
# Array with no elements
my @w = (); 
  
# Statement within 'if' will be executed 
# only if the array is not empty 
if (@w
    print "Geeks"

Output:
No Output

Here, nothing is printed as the stated Array is empty. So, the code does not displays the content of the if-statement.

Program 2:




#!/usr/bin/perl 
  
# Program of if-statement in Scalar Context 
use strict; 
use warnings; 
use 5.010; 
  
# An Array of elements
my @w = ('G', 'f', 'G'); 
  
# Statement within 'if' will be executed 
# only if the array is not empty 
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:




#!/usr/bin/perl 
# Program to Read input from user
use strict; 
use 5.010; 
    
# Asking the user to provide input  
print "Enter your name:\n"
    
# Getting input from user  
my $y = <STDIN>; 
  
# Printing the required output 
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.


Article Tags :