- 01Setting up (what you'll need)
- 02Your first output (print)
- 03Variables (scalar variables, $)
- 04Branching (the if statement)
- 05Repeating (the for loop)
- 06Writing a subroutine (a function)
- 07Repeating (the while loop)
- 08Working with arrays (@)
- 09Working with hashes (%)
- 10Regular expression matching
- 11Substitution with regular expressions (s///)
- 12Working with strings
- 13References
- 14Anonymous subroutines
- 15File input and output, the basics
- 16Modules and use strict / warnings
- 17The ternary operator
- 18Sorting (sort)
- 19Packages and the basics of object orientation
- 20[Project] Build a small inventory system
The ternary operator
This lesson covers the ternary operator, which fits an if statement onto one line.
Writing condition ? value_if_true : value_if_false picks between two values on a single line.
The sample code uses the ternary operator to decide between "Pass" and "Fail" based on my $score = 75;.
A common early stumble is overusing it until the code becomes hard to read. It is handy for a simple branch, but nesting them tends to make things less clear rather than more.
In professional work, the ternary operator is used wherever you want to keep code compact — a short conditional assignment to a variable, for instance.
๐งช This site can't compile or run Perl directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
