How to Debug Perl

來源:互聯網
上載者:User
Perl Debug
[From] http://goldenink.com/perl/perldebug.html

The concepts of the perl debug are familiar to most programmers. Trace, breakpoint, and conditional breakpoint (watchpoint) are effective means with which to find problems in your perl script.

Common errors in perl

Check for these common errors before you try to debug a program: no semi-colon at end of line. matching pairs of delimiters: ( ), { }, [ ]. Using = when you mean ==. Using == when you mean eq. Using == when you mean =~. Disagreement of field, array or hash names (remember $data,$data[1],$data{1} and $Data are four different entities. Disagreement of subroutine names between execution and definition. Incorrect use of if, elsif, else. Not checking your program (and correcting the errors) using the -w switch.

Using the -w switch

perl -w myprogram

Both this useful command and the output from this command are frequently overlooked by experienced programmers. I have, on more than one occasion, spent an hour or 2 looking for a problem when correcting the errors displayed on the -w switch would have corrected or led me to the correction of the problem in a couple of minutes.

The debug command

perl -d myprogram

Essentially the -d switch allows you to communicate with the perl executable, and allows the perl executable to communicate with you.

Stepping up to the plate l - lists the next few lines p [variable name] - lets you print the value of a variable to the command line q - I quit. r - returns from current subroutine If you have written your program as a few subroutines, this is handy for running through each and checking the return. n - executes the next statement at this level Means you don't have to go through those boring subroutine calls. If you don't know what I just wrote, get into a program and try it a couple of times. You'll get the hang of it. s - The step command Okay, this is the key for simple debugging. Crank up your program in the debugger and step through it. If its a small program, this and the r command can take care of most of your needs. By the way, once you enter the s command, just hit enter to repeat the command.

Breaking away

Breakpoints allow you to specify the next stopping point in a program. For example, lets say I know my problems at line 65, I might insert a breakpoint at line 60 and step through the final five lines of code before the problem. You just saved hitting the enter key 60 times.

b - Set a breakpoint

Once you have started the perl debugger you may set a breakpoint at any executable line number.

b 20 - Sets a breakpoint at line 20.
b subroutinename - handy little deviation. If you know the subroutine name, this breaks on the first line inside the routine.
b 20 condition - depending on your background, this is called a conditional breakpoint or watchpoint. Perl breaks when the condition you specify is met.
Examples b 20 x>30 Breaks at line 20 when x is greater than 30 b 20 x=~/foo/i Breaks at line 20 when x contains "foo" To start the program use c (continue), which runs the program to the first breakpoint or watchpoint.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.