Perl | Boolean Values

• Difficulty Level : Medium
• Last Updated : 21 Aug, 2021

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

 # Perl Code to demonstrate the boolean values # variable assigned value 0\$k = 0; # checking whether k is true or falseif (\$k){    print "k is True\n";}else{    print "k is False\n";} # variable assigned value 2\$m = 2; # checking whether m is true or falseif (\$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.

• Example:

Perl

 # Perl Code to demonstrate the True values # variable assigned value 5\$a = 5; # checking whether a is true or falseif (\$a){    print "a is True\n";}else{    print "a is False\n";} # string variable assigned white# space character\$b = ' '; # checking whether b is true or falseif (\$b){    print "b is True\n";}else{    print "b is False\n";} # string variable assigned 'false'# value to it\$c = 'false'; # checking whether c is true or falseif (\$c){    print "c is True\n";}else{    print "c is False\n";} # string variable assigned "0\n"# value to it\$d = "0\n"; # checking whether d is true or falseif (\$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.

• Example:

Perl

 # Perl Code to demonstrate the False values # variable assigned value 0\$a = 0; # checking whether a is true or falseif (\$a){    print "a is True\n";}else{    print "a is False\n";} # string variable assigned empty string\$b = ''; # checking whether b is true or falseif (\$b){    print "b is True\n";}else{    print "b is False\n";} # string variable assigned undef\$c = undef; # checking whether c is true or falseif (\$c){    print "c is True\n";}else{    print "c is False\n";} # string variable assigned ""# value to it\$d = ""; # checking whether d is true or falseif (\$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.

• Example:

Perl

 # Perl Program demonstrate the conditional check # variable initialized with string\$x = "GFG"; # using if statementif (\$x eq "GFG"){    print "Return True\n";}else{    print "Return False\n";}

Output:

Return True

My Personal Notes arrow_drop_up