Open In App

Perl | undef and the defined function

Improve
Improve
Like Article
Like
Save
Share
Report

undef is used for those variables which do not have any assigned value. One can compare it with NULL(in Java, PHP etc.) and Nil(in Ruby). So basically when the programmer will declare a scalar variable and don’t assign a value to it then variable is supposed to be contain undef value. In Perl, if the initial value of a variable is undef then it will print nothing.
Example: 
 

Perl




# Perl program for undef variable
# variable which declared without
# initial value
 
# variable x without initial value
my $x;
 
# printing its value
print "The value of x is = ${x}";


Output: 
 

The value of x is = 

undef() function: undef is used to reset the value of any variable. It can be used with or without the parentheses. It means that parentheses are optional while using this function.
 

  • Example:
     

Perl




# Perl program to illustrate
# the undef() function
 
# a variable with initial value 10
$k = 10;
 
# displaying its initial value
print "The value of variable k is ${k}\n";
 
 
# undef the variable
undef $k;
 
# value after the undef function
print "The value of variable k is ${x}\n";
 
# a variable with initial value 20
$m = 20;
 
# displaying its initial value
print "The value of variable m is ${m}\n";
 
# undef the variable
# you can also use
# $m = undef;
$m = undef();
 
# value after the undef function
print "The value of variable m is ${m}\n";


  • Output: 
     
The value of variable k is 10
The value of variable k is 
The value of variable m is 20
The value of variable m is 

defined() function: This function is used to check whether a value is assigned to the variable or not. It will return True if the value is assigned to the variable otherwise it will return false. 
 

  • Syntax: 
     
defined $variable_name
  • Example:
     

Perl




# Perl program to illustrate
# the defined() function.
 
# a variable assigned value 15
$k = 15;
 
# To check if variable is defined or not
if (defined $k)
{
     print "k is defined\n";
}
else
{
    print "k is not defined\n";
}
 
 
# undef the variable
$k = undef;
 
# To check if the variable is defined or not
if (defined $k)
{
    print "k is defined\n";
}
else
{
    print "k is not defined\n";
}


  • Output: 
     
k is defined
k is not defined

Note: Although undefined variables have no defined value, but they can take null value during the operation. For example, if user is adding two variables x and y in which x is 5 and y is undefined, then the result would remain 5 because y will take up the value 0. Similarly, if user concatenates two strings strx and stry with strx as value “GGF” and stry is undef, the result would be “GFG”, as stry takes the value of a blank string “”.
 

  • Example: 
     

Perl




# Perl program to demonstrate behaviour
# of undef variables, during operation
# like arithmetic, concatenation etc.
 
# first variable assigned with value
$x = 125;
 
# second variable with no value
my $y;
 
# arithmetic operation
$z = $x + $y;
 
# displaying sum
print "The sum of x and y is ${z}\n";
 
# declaring strx and assigned a value to it
$strx = "GFG";
 
# declaring stry without assigned value
my $stry;
 
# string concatenation
$strz = $strx.$stry;
 
# after concatenation
print "After concatenation of strx and stry  ${strz}\n";


  • Output: 
     
The sum of x and y is 125
After concatenation of strx and stry  GFG

 



Last Updated : 14 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads