X hits on this document

PDF document

Running and Debugging Perl - page 23 / 30





23 / 30

Running and Debugging Perl

In any case, the smaller you can make your demonstration code, the better – especially if you're planning on asking someone else about it. The smaller your haystack, the more chance you have of finding a needle in it. Furthermore, plenty of people may be willing to help you if you can produce two lines that demonstrate a problem – fewer will if they think you expect them to debug your entire program.

Here are a some other problems that can cause weirdness without actually causing an error:


Is there a problem with context? Always make sure you know what you're expecting from a function – whether you want an array or a scalar – and ensure that you're collecting the result in the appropriate type of variable.


Has a variable you've declared with my gone out of scope and become undefined or returned to its original value (from before you my'd it)? Remember that declaring variables my inside a block or loop means you won't be able to get at their value outside of it.


Are you saying something like print (2+3)*5? This would add two to three, print it, and multiply the result of the print by five. Have you forgotten brackets around a list? Having warnings turned on will help you pick up on most of these sorts of thing, but be careful. Whenever in doubt, bracket more than you need to.

Using the Debugger

The Perl debugger isn't a separate program, but a special mode under which perl runs – to enable it, you simply pass the -d switch. As it's a special mode for running your program, you won't get anywhere unless your program compiles correctly first. What the debugger will help you do is to trace the flow of control through your program and allow you to look at variables' values at various stages of operation.

When you start your program in the debugger, you should see something like this:

>perl –d nl3.plx Default die handler restored.

Loading DB routines from perl5db.pl version 1.07 Editor support available.

E n t e r h o r ` h h ' f o r h e l p , o r ` p e r l d o c p e r l d e b u g ' f o r m o r e h e l p .

main::(nl3.plx:6): DB<1>

my $lineno;

That line 'DB<1>' is the debugger prompt. Here is a partial list of things you can do at that point:

Command T

Description Obtain a 'call trace' of all the subroutines perl is currently processing. This

will tell you how you got to be where you are.


Step to the next line as you go one line at a time through your program. Table continued on following page


Document info
Document views101
Page views101
Page last viewedWed Jan 18 22:40:12 UTC 2017