O'Reilly logo

Sams Teach Yourself Perl in 21 Days, Second Edition by Rafe Colburn, Laura Lemay

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required