Conditionals

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.