Perl | undef and the defined function
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 intial value of a variable is undef then it will print nothing.
Example:
# 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 program to illustrate# the undef() function# a variable with initial value 10$k= 10;# displaying its initial valueprint"The value of variable k is ${k}\n";# undef the variableundef$k;# value after the undef functionprint"The value of variable k is ${x}\n";# a variable with initial value 20$m= 20;# displaying its initial valueprint"The value of variable m is ${m}\n";# undef the variable# you can also use# $m = undef;$m=undef();# value after the undef functionprint"The value of variable m is ${m}\n";chevron_rightfilter_noneOutput:
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 program to illustrate# the defined() function.# a variable assigned value 15$k= 15;# To check if variable is defined or notif(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 notif(defined$k){print"k is defined\n";}else{print"k is not defined\n";}chevron_rightfilter_noneOutput:
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 program to demonstrate behaviour# of undef variables, during operation# like airthmetic, concatenation etc.# first variable assigned with value$x= 125;# second variable with no valuemy$y;# airthmetic operation$z=$x+$y;# displaying sumprint"The sum of x and y is ${z}\n";# declaring strx and assigned a value to it$strx="GFG";# declaring stry without assigned valuemy$stry;# string concatenation$strz=$strx.$stry;# after concatenationprint"After concatenation of strx and stry ${strz}\n";chevron_rightfilter_noneOutput:
The sum of x and y is 125 After concatenation of strx and stry GFG
Recommended Posts:
- Perl | defined() Function
- Perl | sin() Function
- Perl | oct() Function
- Perl | cos() Function
- Perl | uc() Function
- Perl | int() function
- Perl | hex Function
- Perl | chr() Function
- Perl | tell() Function
- Perl | ord() Function
- Perl | log() Function
- Perl | abs() function
- Perl | each() Function
- Perl | exp Function
- Perl | quotemeta() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Improved By : Akanksha_Rai

