1. Perl
  2. Syntax
  3. here

Perl boolean

Explains boolean in Perl.

False values

There are five false values in Perl. Undefined value, equal to 0 as number, empty string, string "0", a empty list are false value.

1 undef Undefined value
2 "" Empty string
3 0 Equal to 0 as a number.
4 "0" String 0
5 () Empty list

True values

The true value in Perl is all values except the above.

The following is the example of true values.

# 1
1;

# String
"Hellow"

# object
my $obj = Point->new;

# String "0.0"
"0.0"

Note that "0.0" is a true value. Only "0" is a false value in strings which has more than zero length.

Example program

This is an example boolean value.

# Boolean example
my $var; # False. The variable immediately after definition is undef
$var = undef; # False.

$var = 0; # False.
# $var = 0.0 A number equivalent to 0 is false.
$var = 1; # True.

$var = ''; # False
$var = '0'; # False.
# $var = '0.0' True. True for strings except '0' and''.

See the following article for the combination with if statement.

Related Informatrion