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.
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:
'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.
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.
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.
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.
There can be only one action per line.
'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.
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.
< and > commands
< 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 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.
< action to be performed;
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 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,
> action to be performed;
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
DB<9> > ;
This will delete the action specified using
Displaying Line Actions Using the ‘L’ Command
'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.
DB<3> L a;
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.