How to Debug Perl

Source: Internet
Author: User
Tags perl debugger
Perl Debug
[From] http://goldenink.com/perl/perldebug.html

The concepts of the Perl debug is familiar to most programmers. Trace, breakpoint, and conditional breakpoint (watchpoint) is effective means with which to find problems in your Perl SC Ript.

Common Errors in Perl

Check for these common errors before-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 is four different entities. Disagreement of subroutine names between execution and definition. Incorrect use of If, elsif, else. The 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 is frequently overlooked by experienced programmers. I has, on more than one occasion, spent an hour or 2 looking for a problem when correcting the errors displayed on The-w Switch would has 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 Communicat E with you.

Stepping up to the plate l-lists the next few lines p [variable name]-lets your print the value of a variable to the CO Mmand Line Q-i quit. R-returns from the subroutine If you have written your program as a few subroutines, this is the handy for running Throu GH each and checking the return. N-executes the next statement at the "This" Means you don ' t has to go through those boring subroutine calls. If you don't know what I just wrote, get to 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 the most of the your needs. By the the-the-once you enter the command, just hits enter to repeat the command.

Breaking away

Breakpoints the Specify the next stopping point in a program. For example, lets say I know my problems @ line, I might insert a breakpoint on line and step through the final FIV E lines of code before the problem. You just saved hitting the ENTER key.

b -Set a breakpoint

Once you have started the Perl debugger, set a breakpoint at any executable line number.

B -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 Condition -depending on your background, which is called a conditional breakpoint or watchpoint. Perl breaks when the condition your specify is met.
Examples b x>30 Breaks at line if X is greater than b x=~/foo/i Bre AKS at line if X contains "foo" to start of the program use C (continue), which runs the program to the first breakpoint or Watchpoint.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.