The if Statement

To control whether statements are executed based on a condition in a Perl program, you usually use an if statement. The syntax of an if statement is as follows:

if (expression) block
					

The statement works like this: If the expression evaluates to true, the block of code is run. If the expression is false, the block of code is not run. Remember that the block includes the braces. Consider this example:

if ( $r == 5 ) {
    print 'The value of $r is equal to 5.';
}

The expression being tested is $r == 5. The == symbol is an equality operator. If the two operands on either side—$r and 5—are numerically equal to one another, the expression is considered to be true, and the print statement is executed. If $r is not equal to 5, the ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD 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.