The Wayback Machine - https://web.archive.org/web/20240927090615/https://www.geeksforgeeks.org/perl-boolean-values/
Open In App

Perl | Boolean Values

Last Updated : 21 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 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 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 false
if ($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 false
if ($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 false
if ($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 false
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.

  • Example: 

Perl




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

  • Example: 

Perl




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


Output: 

Return True

 



Previous Article
Next Article

Similar Reads

Perl | split() Function
split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc.. The best thing about this function that user can specify how many sec
8 min read
Perl | Scalars
A scalar is a variable that stores a single unit of data at a time. The data that will be stored by the scalar variable can be of the different type like string, character, floating point, a large group of strings or it can be a webpage and so on.Example : C/C++ Code # Perl program to demonstrate # scalars variables # a string scalar $name = "
2 min read
Number Guessing Game using Perl
The Number guessing game is all about guessing the number randomly chosen by the computer in given number of chances. Functions to be used : rand n : This function used to generate a random number between 0 to n. And this function always returns the floating point number. So its result is converted to integer value explicitly. Chomp() : This functi
4 min read
Perl | Removing leading and trailing white spaces (trim)
Removing unwanted spaces from a string can be used to store only the required data and to remove the unnecessary trailing spaces. This can be done using the trim function in Perl. The trim function uses a regular expression to remove white spaces. It is not a library function but defined by the user whenever required. The types of trim functions ar
2 min read
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 initial value of a variable is undef then it wil
3 min read
Perl | Automatic String to Number Conversion or Casting
Perl has a different way to deal with the operators because here operator defines how the operands will behave but in other programming languages, operands define how an operator behaves. Casting refers to the conversion of a particular variable's data type to another data type. For example, if there is a string "1234" and after converting it to in
4 min read
Perl | Multi-line Strings | Here Document
A string in Perl is a scalar variable and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. Multi-line string is required in a scenario where the user wants a complete paragraph or a group of paragraphs as a string and for that, he or she needs to preserve the
4 min read
Perl | Count the frequency of words in text
Counting frequency of all words of a string is a basic operation for any programming language. The frequency of each word of the text can be counted and stored in a hash for further use. In Perl, we can do this by firstly splitting the words of the string into an array. We use the function split / / which splits the string with ' '. However the bla
3 min read
Boolean Indexing in Pandas
In boolean indexing, we will select subsets of data based on the actual values of the data in the DataFrame and not on their row/column labels or integer locations. In boolean indexing, we use a boolean vector to filter the data. Boolean indexing is a type of indexing that uses actual values of the data in the DataFrame. In boolean indexing, we can
6 min read
Java.lang.Boolean Class in Java
Java provides a wrapper class Boolean in java.lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a boolea
8 min read
Stream.of(T... values) in Java with examples
Stream of(T... values) returns a sequential ordered stream whose elements are the specified values. A sequential stream work just like for-loop using a single core. On the other hand, a Parallel stream divide the provided task into many and run them in different threads, utilizing multiple cores of the computer. Syntax : static <T> Stream<
2 min read
Storage of integer and character values in C
Integer and character variables are used so often in the programs, but how these values are actually stored in C are known to few. Below are a few examples to understand this: Taking a positive integer value as char: #include <stdio.h> int main() { char a = 278; printf("%d", a); return 0; } Output: 22 Explanation: First, compiler co
2 min read
Does STL priority queue allow duplicate values?
Yes, in C++ priority_queue, we may have duplicate values. // C++ program to demonstrate that duplicate // values are allowed in a priority queue // (with maximum value at the top) #include <bits/stdc++.h> using namespace std; int main() { priority_queue<int> pq; pq.push(30); pq.push(5); pq.push(30); cout << pq.top() <<
1 min read
Longest subarray with difference exactly K between any two distinct values
Given an array arr[] of length N and an integer K, the task is to find the longest subarray with difference between any two distinct values equal to K. Print the length of the longest subarray obtained. Otherwise, if no such subarray is obtained, print -1. Examples: Input: arr[] = {0, 0, 1, 1, 3, 3, 3}, K = 1 Output: 4 Explanation: The subarray {0,
12 min read
Count possible values of K such that X - 1 and Y - 1 modulo K is same
Given two numbers X and Y, the task is to find the count of integers K which satisfies the equation (X - 1) % K = (Y - 1) % K. Examples: Input: X = 2, Y = 6Output: 3Explanation:K = {1, 2, 4} satisfies the given equation. Therefore, the count is 3. Input: X = 4, Y = 9Output: 2 Approach: Follow the steps below to solve the problem: The idea is to fin
4 min read
Count sequences of given length having non-negative prefix sums that can be generated by given values
Given two integers M and X, the task is to find the number of sequences of length M that can be generated comprising X and -X such that their respective counts are equal and the prefix sum up to each index of the resulting sequence is non-negative. Examples: Input: M = 4, X = 5Output: 2Explanation:There are only 2 possible sequences that have all p
7 min read
Sort Array according to modulus of their values with their frequencies
Given an array arr containing N positive integers, sort them according to the increasing modulus of their values with their frequencies. Example: Input: arr[]={1, 1, 5, 3, 2, 3, 3, 3, 4, 5, 4, 5}Output: 2 4 4 1 1 5 5 5 3 3 3 3 Explanation:The elements are sorted in the following order:2 % frequency(2) = 2 % 1 = 04 % frequency(4) = 4 % 2 = 01 % freq
6 min read
Default Array Values in Java
If we don't assign values to array elements and try to access them, the compiler does not produce an error as in the case of simple variables. Instead, it assigns values that aren't garbage. Below are the default assigned values. S. No.DatatypeDefault Value1booleanfalse2int03double0.04Stringnull5User-Defined Typenull Example: Java Code // Java prog
2 min read
Find if a 2-D array is completely traversed or not by following the cell values
You are given a 2-D array. We have to traverse each and every cell of the given array by following the cell locations then return true else false. The value of each cell is given by (x, y) where (x, y) is also shown next following cell position. Eg. (0, 0) shows starting cell. And 'null' shows our final destination after traversing every cell. Exam
7 min read
A shorthand array notation in C for repeated values
In C, when there are many repeated values, we can use a shorthand array notation to define array. Below program demonstrates same. // C program to demonstrate working of shorthand // array rotation. #include <stdio.h> int main() { // This line is same as // int array[10] = {1, 1, 1, 1, 0, 0, 2, 2, 2, 2}; int array[10] = {[0 ... 3]1, [6 ... 9]
1 min read
Assigning long values carefully in Java to avoid overflow
Predict the output of the following program public class LongDivision { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } } Solution: Both the divisor and the dividend are of type long, which is easi
3 min read
Features of C Programming Language
C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for sys
3 min read
Java Arithmetic Operators with Examples
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: Arithmetic Operator
6 min read
Data encryption standard (DES) | Set 1
This article talks about the Data Encryption Standard (DES), a historic encryption algorithm known for its 56-bit key length. We explore its operation, key transformation, and encryption process, shedding light on its role in data security and its vulnerabilities in today's context. What is DES?Data Encryption Standard (DES) is a block cipher with
15+ min read
Introduction to Internet of Things (IoT) - Set 1
IoT stands for Internet of Things. It refers to the interconnectedness of physical devices, such as appliances and vehicles, that are embedded with software, sensors, and connectivity which enables these objects to connect and exchange data. This technology allows for the collection and sharing of data from a vast network of devices, creating oppor
9 min read
Block Cipher modes of Operation
Encryption algorithms are divided into two categories based on the input type, as a block cipher and stream cipher. Block cipher is an encryption algorithm that takes a fixed size of input say b bits and produces a ciphertext of b bits again. If the input is larger than b bits it can be divided further. For different applications and uses, there ar
5 min read
Carrier Sense Multiple Access (CSMA)
This method was developed to decrease the chances of collisions when two or more stations start sending their signals over the data link layer. Carrier Sense multiple access requires that each station first check the state of the medium before sending. Prerequisite - Multiple Access Protocols Vulnerable Time: Vulnerable time = Propagation time (Tp)
6 min read
Carry Look-Ahead Adder
The adder produce carry propagation delay while performing other arithmetic operations like multiplication and divisions as it uses several additions or subtraction steps. This is a major problem for the adder and hence improving the speed of addition will improve the speed of all other arithmetic operations. Hence reducing the carry propagation de
5 min read
Phases of a Compiler
Prerequisite - Introduction of Compiler design We basically have two phases of compilers, namely the Analysis phase and Synthesis phase. The analysis phase creates an intermediate representation from the given source code. The synthesis phase creates an equivalent target program from the intermediate representation. A compiler is a software program
7 min read
goto Statement in C
The C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read
Article Tags :
Practice Tags :