Perl: Common Debugging commands

Source: Internet
Author: User
Tags perl interpreter perl debugger perl script

The Perl debugger used in this article is the perl interpreter itself. You can enable the debugging function through the-D switch. This article will not cover all Debugging commands, but will explain some of our commonly used Debugging commands, hoping to help Perl-loving friends.

Turn on debugging

Perl is an interpreted language. To run a Perl script, you can directly explain and execute commands such as Perl test. pl in the command line. If you want to debug our scriptProgramYou can add the-D option with the command Perl-d test. pl. After you press enter, the program stops at the DB <1>, as shown in:

It can be seen that the second line (test. pl 2) of the Program executed to the main program)

 

Insert/View/delete a breakpoint

You can run the "B" command to insert a breakpoint. The command format is "B row number/function name condition". You can set a breakpoint at <1> in the central dB, as shown in:

 Set the breakpoint at setinitvalue in the sub-process.

Use the l command to view the Set breakpoint. Note that the command is in uppercase and the command is not executed in lowercase.CodeAs shown in:

The breakpoint is set at 21 rows.

 

Run the "B" command to delete the Set breakpoint. The format is "B line number | *. You can specify a line number to delete the breakpoint or use the wildcard" * "to delete all breakpoints, as shown in:

Delete a breakpoint at 21 rows

 

Add/View/delete monitoring

Adding monitoring is very important for debugging programs. In this example, a variable named $ Val is defined in the script. The initial value is 1 and the value is changed to 12 in the setinitvalue function. By adding monitoring to it, you can understand how to add or remove monitoring in Perl.

Add a monitoring expression $ val = 1 with the command W $ val = 1, as shown in:

Add a monitoring rule for expressions

 

To view the value of the current expression, run the p command in the format of P expr. To view the value of $ val = 1, run the command p $ val = 1, as shown in:

The current expression value is 1, that is, true

You can run the W (uppercase letter) command to delete monitoring data. The format is W expr | *. You can delete a specified expression or use the wildcard * to delete all monitoring values, as shown in:

Delete monitoring on expression $ val = 1

 

Step)

Step-by-step execution is in single-step debugging. When a sub-process is encountered, the sub-process is executed and the command is S, as shown in:

Step into execution and enter the setinitvalue sub-process

 

Step-out execution(Step out)

During out-of-step execution and single-step debugging, when other sub-processes are called, other rows of the sub-process are directly executed instead of in the sub-process, then, the control is returned to the subprocess caller. The example program calls the addret sub-process in the setinitvalue sub-process. The differences are as follows:

Sample Code

Debug the program to the setinitvalue sub-process to call the addret sub-process

Use the S command to step into the addret sub-process.

Run the N command to directly execute addret and return

 

Return from subroutine)

This command is actually quite useful and may not be felt in a simple program. However, when you debug a large Perl program, you will find that the program you debug often references many other custom or third-party modules, in addition, the debugged program may frequently enter the code of these modules. To save time and convenience, we will choose to jump out of the Command Option to directly jump out of this module or the sub-process that is currently in progress.

The command is R. In the preceding example, the S command jumps into the addret sub-process and returns the result using the R command, as shown in:

Use the R command to directly jump out of the addret sub-process

 

Exit debugging 

Run the Q command to exit debugging, as shown in:

 

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.