HTML CSS PHP PERL

Q&A

 
Previous Table of Contents Next

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 pagewhich you can view by typing perldoc perlsyn at a command promptcontains 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 like, within memory restrictions of your system. Usually, however, if you have deeply nested loops, it a sign that you should approach the problem differently.

Q3:

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

A3:

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.

    Previous Table of Contents Next
    © 2000- NIV