Perl 101 (Part 1) - The Basics - To Err Is Human...To Debug, Divine! (
Page 4 of 5 )
Perl
also comes with a very powerful debugger, which alerts you to mistakes in your
code. As an example, try omitting the last double quote from the example above
and run the script again. You should see something like this:
Can't find string terminator '"' anywhere
before EOF at ./test.pl line 5.
As you can see, the error message tells you pretty much all you need to know to
find and squash the bug. Of course, all Perl's error messages aren't quite that
informative - some of them are liable to sound a lot like Greek spoken
backwards, especially if you're a novice. And so it's a good idea to use the
"-w" flag when running your program - this flag tells the debugger to print
extra warnings when it encounters an error.
Take a look at the following
example, which runs the Perl script above with the "-w" flag:
#!/usr/bin/perl -w
# Synthesis
print ("Hello there! And what are you doing ",
1+1, "night, baby?n);
If you run this program, here's what you'll see:
syntax error at TEST line 5, near "print"
Unquoted string "doing" may clash with future reserved
word at TEST line 5.
String found where operator expected at TEST line 5,
near "doing ", 1+1, ""
(Do you need to predeclare doing?)
Bare word found where operator expected at TEST line
5, near "", 1+1,
"night"
(Missing operator before night?)
Unquoted string "night" may clash with future reserved
word at TEST line 5.
Unquoted string "baby" may clash with future reserved
word at TEST line 5.
Unquoted string "n" may clash with future reserved
word at TEST line 5.
Execution of TEST aborted due to compilation
errors.
Of course, there is sometimes such a thing as too much data...