Q&A

Q1:I'm familiar with another programming language, C, which has a switch (or case) statement. Where is Perl's switch statement?
A1: Perl doesn't have one! Perl provides such a variety of tests that figuring out the best syntax for a switch statement is nightmarish. The simplest way to emulate a switch statement is as follows:
if ($variable_to_test == $value1) {
    statement1;
} elsif ($variable_to_test == $value2) {
    statement2;
} else {
    default_statement;
}

The online syntax manual page—which you can view by typing perldoc perlsyn at a command prompt—contains many clever examples of how to emulate a switch statement in Perl, some with very switch-like syntax.

Q2:How many for (while, if) blocks can I nest inside each other?
A2: As many as you ...

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.