Lesson 05 of 55
8 mins readPython Booleans, Truth Values & defined()
In Plain English
Perl does not have a distinct boolean data type. The only false values in Perl are: `0`, `"0"`, `""` (empty string), and `undef`. Everything else evaluates to true.
Deep Dive: How It Works
The Falsy Four: 0, "0", "" (empty string), and undef (undefined).
Truthy Values: Any non-zero number, non-empty string other than "0" (e.g. "0.0", "false", "00" are truthy).
defined() Function: Returns true if a variable has a value other than `undef`.
Defined-Or (//): Modern Perl operator that defaults when undefined ($val // "default").
Core Rules to Remember

Only 4 Falsy Values: 0, "0", "", and undef are the only false values in Perl.

defined vs Truth: 0 is defined, but falsy. Use `defined($var)` to check for existence.
Live Interactive Example
Hit Run Code to see it liveTesting Truth and Definedness
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: 0 is defined but falsy. // defaults when a variable is undef.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Defined-Or Default Value
Declare an unassigned variable `my $config_port;`
Use the defined-or operator `//` to set `my $port = $config_port // 8080;`.
Print `"Port: $port\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.