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 example

filter_none

edit
close

play_arrow

link
brightness_4
code

$i = 0;
for($i = 0; $i <= 9; $i++)
{
      
}
print($i);

chevron_right


Above is a basic program of for loop. Now if we specify a line action at line 4 asking the debugger to print the value of i after each iteration then we could check that whether the loop is executing correctly or not. So in this way, they help in debugging.
Thus to specify these line actions Perl Debugger provides you the feature of Line Action Commands. These commands help in specifying the line actions. The Line Action Commands are the statements that we specify to be executed whenever the program execution reaches a specified line. The most common line actions are printing the value of a variable and resetting an incorrect value of the variable to the desired value.
The following are the line action commands:



  1. ‘a’ command
  2. ‘A’ command
  3. ‘<‘ command
  4. ‘>’ command

‘a’ Command

The 'a' command specifies an action for a particular line of code. This action can be any valid Perl command. The specified action will be performed every time the specified line is executed.

Syntax

a line no. command to be executed [condition]

If no line is specified, then by default the action is performed every time the current line is executed.

Example:
Example2

Output:
Output1

Explanation:
In the above example, the debugger executes the above line as soon as it reaches the ninth line. Thus the above statement will be displayed by the debugger.

To create line actions with multiple lines just specify the statements one after the other. In case you need more than one line to write the complete action to be performed use ‘\’ at the end of first-line.

Example:

Example3
Output:


Output2

You can also specify a condition(It is optional) for the execution of the action. Firstly this condition will be evaluated and then if the result is true then only action will be performed for the specified line, else no action will be performed.
In default cases, when there is no condition, it is considered by the debugger that the condition is already TRUE. Hence, the action is performed every time the execution reaches the specified line.

Note:

There can be only one action per line.

‘A’ Command

The 'A' command is used to delete line actions that were previously specified using 'a' command. It deletes the line action for the line specified in the command.
Syntax:

 A lineno.;

Example:
Example4

In above example, the line action specified for line 11 will be deleted.

Please note that in versions of Perl prior to 5.6.1, the 'A' command deletes all the specified line action.
In version Perl 5.6.1 and after, all the actions are deleted only when the asterisk is given as an argument.

Example

Example5

< and > commands

The < and > commands are useful when you know that one of the variables has the wrong value, but you don’t have any idea about which statement assigns the wrong value to the variable. Thus, by using the < and > command you can print the value of a variable before and after the execution of a statement.


< command
The '<' command is used to specify a line action to be performed after the Perl debugger has finished executing statements and before the debugger further demands for more debugging statements.

Syntax:

 < action to be performed;

Example:
Example6

Output:
Output3

The command given in the above example tells the Perl Debugger to print the specified statement before it again halts the execution of the program and starts debugging.

> command
The '>' command is used to specify an action to be performed before executing further statements of the code. Thus, the action will be performed after a certain line of code is executed,
Syntax:

> action to be performed;

Example:
Example7

Output
Output4

To delete an action specified using < and > command, just re-enter the command used to specify the action.

DB<8> < ;

This will delete the action specified using < command.


DB<9> > ;

This will delete the action specified using > command.

Displaying Line Actions Using the ‘L’ Command

The 'L' command is used to display breakpoints, actions and watchpoints. Thus helping the user to see what all actions, breakpoints, and watchpoints are there in the code. Let us now learn how it is used to display the actions.

Example:

DB<3> L a;

Output6

The use of 'L' command to display action benefits in debugging. For example: If you want to delete a specific action but you don’t know which line it corresponds to. So, you can use the first 'L' command to display all the actions and then use the 'A' command to delete the desired action.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.