The if Statement

Now let’s look at one of the fundamental statements of any language, the if statement. An if statement in Perl evaluates an expression, and if that expression is true, it executes the code within the statement. Here’s an example:

if (2 > 1) { 
    print "2 is greater than 1.\n"; 
} 

As you can see, the expression being evaluated is enclosed in parentheses, and the block of code to be executed appears within braces. If the block of code to be executed consists only of a single statement, you can use a shorthand version of the if statement, which looks like this:

print "2 is greater than 1.\n" if (2 > 1); 

That’s basically a more readable way to write an if statement. It acts just as it reads: “Print something if this condition is ...

Get Sams Teach Yourself CGI in 24 Hours, 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.