Open In App

Perl Stepping Through Programs with a Debugger

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Controlling program execution in Perl can be done by telling the debugger to execute up to a certain specified point in the program, called a breakpoint. These breakpoints enable the user to divide the program into sections and look for errors.

Following are some commands in a debugger that are used to create such breakpoints and the commands that are into execution until a breakpoint is reached.

‘s’ Command

The ‘s’ command tells the debugger to execute the next statement in the program and then stop, allowing the user to examine the program’s state. For example, consider the following code snippet:

10: $GFG = 10;
11: $GeeksForGeeks = 20;
12: $result = $GFG + $GeeksForGeeks;

When the program is stopped at line 12, the ‘s’ command would execute the statement on line 12 and then stop, allowing the user to examine the value of the variable $result.

DB<13> s
main::(debugtest:12): $result = $GFG + $GeeksForGeeks;
DB<14>

‘n’ Command

The ‘n’ command tells the debugger to execute the next statement in the program without stopping unless the statement is a function or subroutine call. For example, consider the following code snippet:

10: $GFG = 10;
11: $GeeksForGeeks = 20;
12: $result = $GFG + $GeeksForGeeks;
13: print $result;

When the program is stopped at line 12, the ‘n’ command would execute the statement on line 12 and then move to the next statement, line 13, without stopping, allowing the user to examine the value of the variable $result after it has been printed.

DB<13> n
30
DB<14>

‘f’ command

The ‘f’ command is used to tell the debugger to execute until the program reaches a specified file. For example, consider the following code snippet:

10: include ‘file1.pl’;
11: $GFG = 10;
12: include ‘file2.pl’;
13: $GeeksForGeeks = 20;

When the program is stopped at line 11, the ‘f file2.pl’ command would tell the debugger to execute the program until it reaches the file file2.pl

DB<13> f file2.pl

Carriage-Return Command

The Carriage-Return Command is used to repeat the last command. This command is useful for quickly executing the same command multiple times. For example, if the last command was ‘n’, pressing the carriage return key would execute the ‘n’ command again.

‘r’ Command

The ‘r’ command is used to tell the debugger to execute the program until it reaches a return statement. For example, consider the following code snippet:

10: sub my_sub {
11: $GFG = 10;
12: $GeeksForGeeks = 20;
13: return $GFG * $GeeksForGeeks;
14: }
15: my_sub();

When the program is stopped at line 11, the ‘r’ command would tell the debugger to execute the program until it reaches the return statement on line 13. This allows the user to examine the value of the variable $GFG * $GeeksForGeeks before it is returned.

DB<13> r
main::(debugtest:13): return $GFG * $GeeksForGeeks;
DB<14>

In summary, these commands allow the user to control the program execution and make it possible to stop and examine the program’s state at a certain point, making it easier to find errors. These commands are useful tools for debugging Perl programs, and they can be used in combination with breakpoints to fine-tune the debugging process. It’s important to note that these commands should be used in conjunction with each other to fully utilize the debugging capabilities of the Perl interpreter.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads