Home > Articles


  • Print
  • + Share This


Q I'm familiar with another programming language, C, which has a switch (or case) statement. Where is Perl's switch statement?

A 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) {
} elsif ($variable_to_test == $value2) {
} else {

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.

Q How many for (while, if) blocks can I nest inside each other?

A As many as you like, within memory restrictions of your system. Usually, however, if you have deeply nested loops, it is a sign that you should approach the problem differently.

Q Help! Perl is giving me the message Unmatched right bracket (or Missing right bracket). The line number reported is the end of the file!

A Somewhere in your program, you've used an open brace ({) without a close brace (}), or vice versa. Perl can sometimes guess where the typo is in your program, but sometimes not. Because control structures can nest arbitrarily deeply, Perl doesn't know you've made a mistake until it unexpectedly reaches the End of File without finding the balancing brace. A good program editor (such as vi, Emacs, or UltraEdit) has features to help you find mismatched braces. Use one.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.