X hits on this document

PDF document

Running and Debugging Perl - page 2 / 30





2 / 30

Chapter 9

Error Messages

There are two types of mistake you can make when programming: a syntax error and a logic error. A syntax error is something like a typo or a result of misunderstanding how to use the language, meaning that your code doesn't actually make sense any more. Since your code isn't properly written in Perl, perl can't understand it and complains about it.

A logic error, on the other hand, is where the instructions you give make perfect sense, but don't actually do what you think they ought to. This type of error is far more dastardly to track down but there are ways and means to do so. For the time being, though, we'll start by looking at the way Perl detects and reports syntax errors:

Try It Out : Examining Syntax Errors

Let's create a few syntax errors, and see how Perl reports them to us. Take the following program, for example:


  • #


use warnings; use strict;

my $a; print "Hello, world." $a=1; if ($a == 1 {

print "\n"; }

As you should be able to see if you look carefully, this contains a number of mistakes. This is what Perl makes of it:

>perl errors.plx Scalar found where operator expected at errors.plx line 8, near "$a"

(Missing semicolon on previous line?) syntax error at errors.plx line 8, near "$a" syntax error at errors.plx line 9, near "1 {" Execution of errors.plx aborted due to compilation errors. >

How it Works What's Perl complaining about? Firstly, it sees something up on line 8:


Well, there's nothing wrong with that. That's perfectly valid code. When we're trying to track down and understand syntax errors, the key thing to remember is that the line number Perl gave us is as far as it got before realizing there was a problem – that doesn't necessarily mean that the line itself has a problem. If, for instance, we miss out a closing bracket, Perl may go all the way to the end of the file before complaining. In this case, though, Perl gives us an additional clue:


Document info
Document views114
Page views114
Page last viewedSun Jan 22 02:11:29 UTC 2017