19
3.2.2 Error Messages
Fixing errors is an essential step in writing programs. After you've written and edited a program, the next step is to run it to
see if it works. Very often, you'll find that you've made some typographical error, like forgetting to put in a semicolon. As a
result, your program isn't valid, and you'll get various error messages from the system. You then have to read the error
messages and reedit your program to repair the offending code.
These error messages are sometimes rather cryptic. In the event of an error, the Perl interpreter may have some trouble
knowing exactly where you went wrong. It may only recognize that there is something wrong. So it guesses where the
problem is, and in the process, it may give you some extraneous information.
The most important thing about using error messages is to look at the first one or two error messages and ignore the rest; fix
the top problems, and try running the program again. Error messages are often verbose and can run on for several pages.
Just ignore everything but the first errors reported. Another important point is that the line numbers reported in those first
error messages are usually right. Sometimes they're off by a line, and they're rarely way off. Later on, we'll practice
generating and reading error messages.
3.2.3 Debugging
Perhaps your edits created a valid program, and the Perl interpreter reads in your program and runs it. You find, however,
that the program isn't doing what you want it to do. Now you have to go back, look at the program, and try to figure out
what's wrong.
Perhaps you made a simple mistake, such as adding instead of subtracting. You may have misread the documentation, and
you're using the language the wrong way (reread the documentation). You may simply have an inadequate plan for
accomplishing your goal (rethink your strategy and reprogram that part of the code). Sometimes you can't see what's wrong,
and you have to look elsewhere (try searching newsgroup archives or FAQs or asking colleagues for help).
For errors that are difficult to find, there are programs called debuggers that allow you to run the program step by step,
looking at everything that's happening in the program. (Chapter 6
takes an in-depth look at Perl's debugger.)
There are other tools and techniques you can use. For instance, you can examine your program by adding print statements
that print out intermediate values or results. There are also special helper programs that can observe your program while it's
running and then report about it, telling you, for instance, about where the program is spending most of its time. These tools,
and others like them, are essential to programming, and you need to learn how to use them.
3.3 An Environment of Programs
Programming is an exercise in problem solving. It's an iterative, gradual process. Although it can be done by one person
alone, it's often a social activity (this surprises many newcomers). It requires developing specific problem-solving skills and
learning a few tools. Programming is sometimes tricky and can be frustrating. On the other hand, for those with an aptitude,
there's a great sense of satisfaction that comes from building a working program.
Computer programs can be many things, from barely useful, to aesthetically and intellectually stimulating, to important
generators of new knowledge. They can be beautiful. (They can also be destructive, stupid, silly, or vicious; they are human
creations, after all.) Because writing a program is an iterative, building, gradual process, there can be real satisfaction in
seeing the work unfold from simple beginnings to complete structures. For the beginning student, this gradual unfolding of
a new program mirrors the gradual mastery of the language.
As our culture began writing and accumulating programs in the middle of the 20th century, a programming environment
began to develop. Gradually, we've been accumulating a substantial body of procedural knowledge. Programs often reflect
the fact that they swim in waters populated by many other programs, and beginning programmers can expect to learn a lot
from this environment.
3.3.1 Open Source Programs
As programming has become important in the world, it has also become economically valuable. As a result, the source code
for many programs is kept hidden to protect commercial assets and stymie the competition.
However, the source code for many of the best and most used programs are freely available for anyone to examine. Freely
available source code is called open source. (There are various kinds of copyrights that may attach to open source program
code, but they all allow anyone to examine the source code.) The open source movement treats program source code in a
similar manner to the way scientists publish their results: publicly and open to unfettered examination and discussion.
The source code for these programs can be a wonderful place for the beginning programmer to learn how professional
programmers write. The programs available in open source include the Perl interpreter and a large amount of Perl code, the
Linux operating system, the Apache web server, the Netscape web browser, the sendmail mail transfer agent, and much
more.