In most of the programming language True and False are considered as the boolean values. But Perl does not provide the type boolean for True and False. In general, a programmer can use the term “boolean” when a function returns either True or False. Like conditional statements(if, while, etc.) will return either true or false for the scalar values.
Example:
Perl
$k = 0;
if ( $k )
{
print "k is True\n" ;
}
else
{
print "k is False\n" ;
}
$m = 2;
if ( $m )
{
print "m is True\n" ;
}
else
{
print "m is False\n" ;
}
|
Output:
k is False
m is True
True Values: Any non-zero number i.e. except zero are True values in the Perl language. String constants like ‘true’, ‘false’, ‘ ‘(string having space as the character), ’00’(2 or more 0 characters) and “0\n”(a zero followed by a newline character in string) etc. also consider true values in Perl.
Perl
$a = 5;
if ( $a )
{
print "a is True\n" ;
}
else
{
print "a is False\n" ;
}
$b = ' ' ;
if ( $b )
{
print "b is True\n" ;
}
else
{
print "b is False\n" ;
}
$c = 'false' ;
if ( $c )
{
print "c is True\n" ;
}
else
{
print "c is False\n" ;
}
$d = "0\n" ;
if ( $d )
{
print "d is True\n" ;
}
else
{
print "d is False\n" ;
}
|
Output:
a is True
b is True
c is True
d is True
False Values: Empty string or string contains single digit 0 or undef value and zero are considered as the false values in perl.
Perl
$a = 0;
if ( $a )
{
print "a is True\n" ;
}
else
{
print "a is False\n" ;
}
$b = '' ;
if ( $b )
{
print "b is True\n" ;
}
else
{
print "b is False\n" ;
}
$c = undef ;
if ( $c )
{
print "c is True\n" ;
}
else
{
print "c is False\n" ;
}
$d = "" ;
if ( $d )
{
print "d is True\n" ;
}
else
{
print "d is False\n" ;
}
|
Output:
a is False
b is False
c is False
d is False
Note: For the conditional check where the user has to compare two different variables, if they are not equal it returns False otherwise True.
Perl
$x = "GFG" ;
if ( $x eq "GFG" )
{
print "Return True\n" ;
}
else
{
print "Return False\n" ;
}
|
Output:
Return True
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Aug, 2021
Like Article
Save Article