X hits on this document

PDF document

Running and Debugging Perl - page 4 / 30





4 / 30

Chapter 9

if (1) { print "Hello";

my $file = shift;

if (-e $file)


print "File exists.\n";


This will give us:

>perl braces.plx Missing right curly or square bracket at braces.plx line 12, at end of line syntax error at braces.plx line 12, at EOF Execution of braces.plx aborted due to compilation errors. >

The problem is, our missing brace is only at line 7, but Perl can't tell that. To find where the problem is in a large file, there are a variety of things you can do:

Indent your code as we have done to make the block structure as clear as possible. This won't affect what perl sees, but it helps you to see how the program hangs together, making it more readily obvious when this sort of thing happens.

Deliberately leave out semicolons where you think a block should end, and you'll cause a syntax error more quickly. However, you'll need to remember to add the semicolon if you add extra statements to the block.

Use an editor which helps you out: Editors like vi and emacs automatically flash up matching braces and brackets (called balancing) and are freely available for both UNIX and Windows.

We'll also be looking at some more general techniques for tracking down bugs later on in this chapter.

Runaway String

In a similar vein, don't forget to terminate strings and regular expressions. A runaway string will cause a cascade of errors as code looks like strings and strings look like code all the way through your program. If you're lucky though, Perl will catch it quickly and tell you where it starts – miss off the closing " in line 7 of the above example, and Perl will produce this message amongst the rest of the mess:

(Might be a runaway multi-line "" string starting on line 7)

This is also particularly pertinent when you're dealing with here-documents. Let's look again at the example we saw in Chapter 2:

#!/usr/bin/perl #heredoc.plx use warnings; print<<EOF;

This is a here-document. It starts on the line after the two arrows, and it ends when the text following the arrows is found at the beginning of a line, like this:



Document info
Document views37
Page views37
Page last viewedSat May 14 10:31:39 UTC 2016