Perl | Boolean Values
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 Code to demonstrate the boolean values # variable assigned value 0 $k = 0; # checking whether k is true or false if ($k) { print "k is True\n"; } else{ print "k is False\n"; } # variable assigned value 2 $m = 2; # checking whether m is true or false 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 charatcer in string) etc. also consider true values in Perl.
- Example:
# 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";}chevron_rightfilter_noneOutput:
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 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";}chevron_rightfilter_noneOutput:
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 Program demonstrate the conditional check# variable initialized with string$x="GFG";# using if statementif($xeq"GFG"){print"Return True\n";}else{print"Return False\n";}chevron_rightfilter_noneOutput:
Return True
Recommended Posts:
- Perl | values() Function
- Perl | Basic Syntax of a Perl Program
- Perl vs C/C++
- Perl | cos() Function
- Perl | sin() Function
- Perl | GET vs POST in CGI
- Perl | exp Function
- Perl | hex Function
- Perl | ord() Function
- Perl | eq operator
- Perl | ne operator
- Perl | tell() Function
- Perl | each() Function
- Perl | y Operator
- Perl | q operator
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 : Aman Khare 1



