In this third part of a three-part article series on the basics of Perl programming, you'll learn how to use escape sequences in your programming, and how to use the Perl debugger. This article is excerpted from chapter one of the book Beginning Perl, Second Edition by James Lee (Apress; ISBN: 159059391X).
One thing you’ll soon notice about programming is that you’ll make mistakes; mistakes in programs are called bugs. Bugs are almost entirely unavoidable, and creating bugs does not mean you’re a bad programmer. Windows 2000 allegedly shipped with 65,000 bugs, but then that’s a special case, and even the greatest programmers in the world have problems with bugs. Donald Knuth’s typesetting software TeX has been in use for more than 20 years, and Professor Knuth was still finding bugs until a couple of years ago. Who can tell when all the bugs are out anyway?
While we will be showing you ways to avoid getting bugs in your program, Perl provides you with a tool to help find and trace the causes of bugs. Naturally, any tool for getting rid of bugs in your program is called a debugger. Mundanely enough, the corresponding tool for putting bugs into your program is called a “programmer.”
To use the debugger, start your program with the-doption as in
$ perl -d myprogram.pl
See perldoc perldebugfor information about the debugger.
We’ve started on the road to programming in Perl, and programming in general. We’ve seen our first piece of Perl code, and hopefully, you were able to get it to run.
Programming is basically telling a computer what to do in a language it comprehends. It’s about breaking down problems or ideas into byte-sized chunks (as it were), and examining what needs to be done in order to communicate them clearly to the machine.
Thankfully, Perl is a language that allows us a certain degree of freedom in our expression, and, so long as we work within the bounds of the language, it won’t enforce any particular method of expression on us. Of course, it may judge what we’re saying to be wrong, because we’re not speaking the language correctly, and that’s how the majority of bugs are born. Generally though, if a program does what we want, that’s enough—TMTOWTDI.
We’ve also seen a few ways of making it easy for ourselves to spot potential problems, and we know there are tools that can help us if we need it. We have examined a little bit of what goes on inside a computer, how it sees numbers, and how it sees characters, as well as what it does to our programs when and as it executes them.
Create a program newline.pl containing print "Hi Mom.\nThis is my second program.\n". Run this and then replace\nwith a space or a return and compare the results.
Download the code for this book from the publisher’s website at www.apress.com.