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.


Similar Reads

Perl | Breakpoints of a Debugger
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 the errors. Following are some commands in a debugger which are used to create such breakpoints and the commands
7 min read
Perl | Displaying Variable Values with a Debugger
Debugger in Perl comes up with various features thus making the debugging process quite simple and effective. One of the most powerful features of Perl Debugger is displaying variable values. This feature allows us to display the value of any variable at any time. There are two basic commands to implement this feature: 'X' command 'V' command 'X' c
3 min read
Perl | Line Action Commands in a Debugger
Debugger in Perl provides us the feature of Line Action Commands, but before going deep into them lets first talk what actually actions are: so an action is basically an instruction that is given to the debugger to execute it whenever it reaches a particular line. The reason behind getting these actions performed is to make the debugging easy. For
5 min read
Perl - Listing your Program with a Debugger
Perfect programs are hard to get in the very first attempt. They have to go through various steps of debugging to fix all errors. There are two types of errors – Syntax errors and Logical errors. Syntax errors are easy to fix and are found fast. On the other hand, logical errors are hard to find and fix. Thus, a debugger is required to fix those er
3 min read
Perl - Introduction to Debugger
Sure, here's an introduction to the debugger in Perl: The Perl debugger is a tool that helps you find and fix bugs in your Perl programs. It allows you to step through your code one line at a time, examine variables and expressions, set breakpoints, and much more. The debugger can be used in both command-line and graphical environments. To use the
9 min read
Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar
10 min read
Perl | chomp() Function
The chomp() function in Perl is used to remove the last trailing newline from the input string. Syntax: chomp(String) Parameters: String : Input String whose trailing newline is to be removed Returns: the total number of trailing newlines removed from all its arguments Example 1: C/C++ Code #!/usr/bin/perl # Initialising a string $string = &amp;quo
2 min read
Perl | Backtracking in Regular Expression
In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Firstly, they are used in conditionals to determine w
3 min read
Perl | Searching in a File using regex
Prerequisite: Perl | Regular Expressions Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to host language and are not the same as in PHP, Python, etc. Sometimes these are termed as "Perl 5 Compatible Regular Expressions". To use the Regex, Bindi
6 min read
Perl | Operators | Set - 1
Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Operators Can be categorized based upon their different functionality: Arithmetic OperatorsRelat
12 min read
Article Tags :