Boolean values in Perl

undef true false boolean

Perl does not have a special boolean type and yet, in the documentation of Perl you can often see that a function returns a "Boolean" value. Sometimes the documentation says the function returns true or returns false.

So what's the truth?

Perl does not have specific boolean type, but every scalar value - if checked using if will be either true or false. So you can write

if ($x eq "foo") {
}

and you can also write

if ($x) {
}

the former will check if the content of the $x variable is the same as the "foo" string while the latter will check if $x itself is true or not.

What values are true and false in Perl?

It is quite easy. Let me quote the documentation:

The number 0, the strings '0' and '', the empty list "()", and "undef"
are all false in a boolean context. All other values are true.
Negation of a true value by "!" or "not" returns a special false
value. When evaluated as a string it is treated as '', but as a number, it is treated as 0.

From perlsyn under "Truth and Falsehood".

So the following scalar values are considered false:

  • undef - the undefined value
  • 0 the number 0, even if you write it as 000 or 0.0
  • '' the empty string.
  • '0' the string that contains a single 0 digit.

All other scalar values, including the following are true:

  • 1 any non-0 number
  • ' ' the string with a space in it
  • '00' two or more 0 characters in a string
  • "0\n" a 0 followed by a newline
  • 'true'
  • 'false' yes, even the string 'false' evaluates to true.

I think this is because Larry Wall, creator of Perl, has a general positive world-view. He probably thinks there are very few bad and false things in the world. Most of the things are true.

Comments

I know of no language that would not be made better with the inclusion of true and false constants as a literal part of the language. I suppose the Lisp family skips out on this since it already has them. Nice reminder of how things work in Perl. Now how about NULL? ;)


The way that Perl defines 'false' to be any number of different values is very dangerous. Many Perl functions like defined() return undef, not zero, for 'false'. Attempting to use the return value as an array index then fails. I don't understand why Perl boolean functions would ever return undef instead of zero.


defined() returns 0 or 1, not undef.

Suppose you query a database, and the query errors. This is not the same as returning zero rows. A distinction between the two is very useful.

I can't think of a situation where you would want to use an undef as a numerical 0 array offset. But if you did you could simply write:

$my_array[$offest//0]

example:

my @my_array = (2,3,4); my $offest = undef; $my_array[$offest//0] = 1; print "$_\n" for ( @my_array);

prints

1 3 4


In my version of perl defined returns an empty string or 1. Try this out:

perl -e 'use strict; use warnings; my ($a, $b)=(undef, 37); my ($c, $d)=(defined $a, defined $b); print(defined $_ ? "[$_]\n" : "undef\n") for ($a, $b, $c, $d)' undef [37] [] [1]

Other pages

while loop
wantarray - returning list or scalar based on context
wantarray - returning list or scalar based on context
wantarray - returning list or scalar based on context
wantarray - returning list or scalar based on context
undef, the initial value and the defined function of Perl
undef, the initial value and the defined function of Perl
undef - defined - true -false
true
true
seek - move the position in the filehandle in Perl
seek - move the position in the filehandle in Perl
min, max, sum in Perl using List::Util
min, max, sum in Perl using List::Util
false
Writing to files with Perl
Writing Command line scripts and accepting command line parameters using Moo
What is the status of the current test script?
What are -e, -z, -s, -M, -A, -C, -r, -w, -x, -o, -f, -d , -l in Perl?
Using a queue in Perl
Using a queue in Perl
Traversing the filesystem - using a queue
Traversing the filesystem - using a queue
The ternary operator in Perl
Testing a simple TCP/IP server using Net::Telnet
Solution: display unique rows of a file - video
Skip tests if prerequisites are not installed
Short-circuit in boolean expressions
Short-circuit in boolean expressions
Short-circuit in boolean expressions
Scalar and List context in Perl, the size of an array
Scalar and List context in Perl, the size of an array
Processing command line arguments - @ARGV in Perl
Processing command line arguments - @ARGV in Perl
Processing command line arguments - @ARGV in Perl
Perl/CGI script with Apache2
Perl tutorial
Perl for loop explained with examples
Perl Hash
Perl 4 libraries
Multiple expected values - testing dice
Moo attribute predicate and clearer
Moo and required attributes
Modulino: both script and module in Perl
Loop controls: next, last, continue, break
Is this IP in the given subnet?
Is this IP in the given subnet?
Introduction to Regexes in Perl 5
Introducing test automation with Test::Simple
How to splice a CSV file in Perl (filter columns of CSV file)
How to set default values in Perl
How to return nothing (or undef) from a function in Perl?
How to return nothing (or undef) from a function in Perl?
How to process command line arguments in Perl using Getopt::Long
How to process command line arguments in Perl using Getopt::Long
How to process command line arguments in Perl using Getopt::Long
How to exit from a Perl script?
How to create a Perl Module for code reuse?
How to check if a server is live using Ping?
Getting started with Classic Perl OOP
Getting started with Classic Perl OOP
Getting started - Create skeleton for the SCO clone application
Fetching Pictures from Flickr using Perl
Does the 'all' function of List::MoreUtils really short-circuit?
Does the 'all' function of List::MoreUtils really short-circuit?
Creating bar graphs using Perl GD::Graph
Constructor and accessors in classic Perl OOP
Consider everything not recognizable as a paragraph in Markua
Conditional statements, using if, else, elsif in Perl
Conditional statements, using if, else, elsif in Perl
Conditional statements, using if, else, elsif in Perl
Check several regexes on many strings
Caching data using the Cache module
An extra space can ruin your day
Always use warnings in your Perl code!
Advanced usage of Getopt::Long for accepting command line arguments
Advanced usage of Getopt::Long for accepting command line arguments
Advanced usage of Getopt::Long for accepting command line arguments
Advanced usage of Getopt::Long for accepting command line arguments
Advanced usage of Getopt::Long for accepting command line arguments
A Simple way to download many web pages using Perl

Author

Gabor Szabo (szabgab) Gabor Szabo