Conditionals are used to execute different bits of code based on the value of a given test. If the test is true, a block of code is executed; if the test is false, either execution continues to the next part of the script, or a different block of code is executed. Unlike loops, each block is executed only once.

if, ifelse, and ifelsif

The most common form of conditional is the if, and its variant forms ifelse and ifelsif. As you've seen, the if statement looks like this:

if ( test ) {
   # statements

The test is any expression, evaluated in a boolean scalar context for its truth value. Remember that everything except "", 0, and undef is considered true. If the test is true, the block is executed. If it's false, nothing happens ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.