Perl has a boolean value to use in a conditional expression such as if statements.

Programming languages have true and false values for boolean types.

There are no true or false values or boolean types in Perl.

Then, How can you represent boolean values?

In the below, the number variable compared with 1 and Print the string for the passed condition.

my $number=1;
if($number==1){
print("equal\n");
}
if($number!=1){
print("not equal\n");
}

Output:

equal

What does the condition return?

Let’s print the condition.

print ($number==1),"\n";
print ($number!=1),"\n";

Output:

1

that means, conditional true values are represented as 1 and any other value is false.

Another example

my $number1=11;
my $number2=0;

if($number1){
print("number1 passed\n");
}
else{
  print("number1 not passed\n");

}
if($number2){
print("number2 passed\n");
}
else{
  print("number2 not passed\n");

}

Output:

number1 passed
number2 not passed

From the above example, any number other than zero is treated as conditional true values. remaining values are treated as false values.

Perl Boolean true and false values

let’s see how Perl treats boolean values in conditional expressions.

Any value like a string or numbers other than zero is treated as a true value.

False values

  • number 0 and ‘0’ string values
  • undef values
  • Empty list()
  • ""

True values:

  • any numbered string or numbers other than 0 and "0" values
  • empty []

What boolean value subroute or function return?

Since true and false values do not exist in Perl boolean context.

You can use 1 in place of true and undef in place of false values in subroutes.

Here is an example

sub isSeniorCitizen {
  $age=40
  if ($age>40) {
    return 1;
  else {
    return undef;
  }
}

Does Perl have Boolean?

Like other programming languages, Perl has either no boolean types or true/false values. But, These are represented in the Conditional expression of if-else statements as boolean, Actual values are numbers or strings that contain values that are true values, and other values number and string 0 are false values.

How do I assign a Boolean value in Perl?

To assign a boolean value in Perl, Please follow the below steps

  • Declare a variable, assign a value 1 for true values as given below
my $boolTrue=1
  • declare a variable, and assign a value with zero, that is treated as a false value.
my $boolTrue=0

Does Perl have true and false?

There are no true and false values or strings in Perl for boolean representation. Actual values are numbers or strings that contain values that are true values, other values numbers and string 0 are false values.