Tinkering with Perl

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  30  31  32  33  34  35  36  37  38  39  Next  Skip Forward
Printer-Friendly Version

Flow control

In many programming languages, there is a very powerful kind of statement called the goto statement. Quite often in programming, you are at one part of your code, the list of instructions, and you need to be somewhere else. So programmers would just put a goto in, and voila! you were at that other part of the program.

Well, gotos are very powerful, but they have a bit of a problem with them. If you just use gotos in the way that comes most naturally, the result is something called spaghetti code: the program just goes in and out in one tangled mess that's impossible to understand, just like spaghetti. This results in code that's full of bugs, that's impossible to fix.

Therefore, it is in most cases strongly discouraged to use gotos. Gotos are considered hazardous to your health, and many programmers consider 'goto' to almost be a dirty word.

But wait. A goto is very powerful. If we're going to say "no gotos", shouldn't there be something else to replace them?

And the answer, fortunately, is "Yes." There are other ways to do almost anything that you would do with a goto -- but do it better, and more cleanly. These ways make things much easier to understand, and have less bugs. The bugs that do get in are easier to fix.

There are two basic kinds of structures, that can do almost all of the work done with gotos -- but do it much better. These two structures are called conditionals and loops, and we will explore them. There are also subroutines and functions, which will be covered later, and are also very powerful.

See also:

Statements - Blocks - If-then - If-then-else - If-then-else chains - Loops - Foreach loops - While loops - For loops

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  30  31  32  33  34  35  36  37  38  39  Next  Skip Forward
Printer-Friendly Version