0% found this document useful (0 votes)
27 views

Perl 10 Debugging

This document discusses various debugging techniques in Perl, including using the strict pragma, warnings pragma, command line warning flags, Perl debugger commands, Carp module functions for run-time errors, and the Devel::Peek module for data inspection. It provides an overview of debugging best practices and tools in Perl.

Uploaded by

Nitish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Perl 10 Debugging

This document discusses various debugging techniques in Perl, including using the strict pragma, warnings pragma, command line warning flags, Perl debugger commands, Carp module functions for run-time errors, and the Devel::Peek module for data inspection. It provides an overview of debugging best practices and tools in Perl.

Uploaded by

Nitish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

10/10/2013

Debugging

Contents
 Safety Net
 Warnings
 Command Line Flags
 Fatal Warnings
 Strict Checks – Refs, Vars and Subs
 Diagnostic Messages Pragma
 The “splain” Program
 Perl Debugger
 Debugger Commands
 Carp Module
 Carping, Croaking and Confessing

1
10/10/2013

Contents
 The Devel::Peek Module
 Data Diagnostic tool

Contents
 The grep Operator
 List Operators
 Context

 Initializing Arrays & Hashes


 Reference Syntax
 Auto-vivification

 Other list operators


 Usage of map, grep and foreach

2
10/10/2013

Objectives
 In this session we will learn how to
 Perl‟s pragma to control optional warnings.
 Use warning command line flags
 Strict checks on reference, variables and subroutines
 Perl‟s pragma to extend concise diagnostics
 Using the „splain‟ utility
 Debugging Perl programs using the debugger.
 Understand the common Perl debugger commands.
 Perl‟s pragma for run-time analysis when using modules.
 Data debugging tool for XS Programmers.

Safety Net
 Perl by default is very forgiving.
 To make it more robust it is recommended to start
every program with the following lines:
 use strict;
 use warnings;

3
10/10/2013

Warnings
 Perl pragma to control optional warnings.
 Thewarnings pragma is a replacement for the
command line flag -w
 Difference
 The warnings pragma is limited to the enclosing
block.
 The flag –w is global
 To suppress warnings
 no warnings;

Warnings
 Use Perl‟s $^W variable to enable or disable
warnings in a block of code.
 You can inadvertently change the warning setting.

{
local ($^W) = 0; # Disable warnings
my $a =+ 10;
}

4
10/10/2013

Warnings – Command Line Flags


 Thereare three Command Line flags that can be
used to control when warnings are (or aren't)
produced.
 The –w flag

 The –W flag will enable warnings throughout the


program whether warnings were disabled locally.
 This includes all files included via use or require.

 The –X flag does the opposite of the –W flag.

Fatal Warnings
 The presence of the word "FATAL" in the category
list will escalate any warnings detected into fatal
errors.
use warnings;
# Example to illustrate FATAL warnings.
time;

{
use warnings FATAL => qw(void);
length "abc";
}
join "", 1,2,3;
print "done\n";

 To downgrade a warning that has been escalated


into a fatal use the "NONFATAL" keyword.

5
10/10/2013

Strict Checks
 Perlpragma to restrict unsafe scope, references,
variables and subroutines.
 use strict;
 Strict References
 use strict “refs”;
 Generates a run-time error if you use symbolic
references.
use strict 'refs';
$ref = \$var;
print $$ref; # ok
$ref = "Perl";
print $$ref; # runtime error; normally ok
$file = "STDOUT";
print $file "Hi!"; # error;

Strict Checks
 Strict Variables
 use strict “vars”;
 Generates a compile-time error if you access a
variable that wasn't declared via our or my, or
wasn't fully qualified

use strict ‘vars';


our $var = 125;
my $var = 'Some Text';

print "Private var = $var\n";


local $var = 12.34; # Error

6
10/10/2013

Strict Checks
 Strict Subroutines
 use strict “subs”;
 Generates a compile-time error if you try to use a
bareword identifier

sub Test
{
print "Hello from subroutine.\n";
}

use strict 'subs';


my $ref = Test; # Error
$ref->();

Diagnostic Messages
 Various diagnostic messages are classified as
follows:
 perldoc perldiag

Diagnostic Messages
Character Description
W A warning (optional).
D A deprecation (enabled by default).
S A severe warning (enabled by default).
F A fatal error (trappable).
P An internal error you should never see (trappable).
X A very fatal error (nontrappable).
A An alien error message (not generated by Perl).

7
10/10/2013

Diagnostics Pragma
 Extends concise diagnostics augmenting with
more explicative and endearing descriptions found
in perldiag.
 Enables perl‟s –w flag.
 The -verbose flag first prints out the perldiag
introduction before any other diagnostics.
 Also adds a stack trace to the error message.
 The -traceonly (or just -t) flag turns off the
explanations of warning messages leaving just the
stack traces.
perl -Mdiagnostics=-traceonly my_bad_script

Diagnostics Pragma
 We can enable and disable diagnostics.
 enable diagnostics;
 disable diagnostics;

8
10/10/2013

The “splain” Program


A link to diagnostics.pm module as well as a link
to the diagnostics.pod documentation.

 The –v flag is like “use diagnostics –verbose”


directive.

 The –p flag is like “$diagnostics::PRETTY”


variable.

perl -w test.pl 2> test.out

./splain < test.out

Perl Debugger
 Let us
 Examine source code,
 Set breakpoints,
 Change the values of variables,
 Dump out function-call stack, and so on
 The debugger uses the DB package for its own
state information.

 The –d flag tells the compiler to insert source


information into the parse trees it's about to hand
off to the interpreter.

9
10/10/2013

Perl Debugger
 Thecode must first compile correctly for the
debugger to work on it.

perl -d test.pl

Debugger Commands
 The Perl Debugger uses the following commands.
Debugger Commands
Command Description
h [cmd] Print out a help message.
l List next few lines.
l min+incr List (incr+1) lines starting at min.
l min-max List lines min through max.
l line List a single line.
l subname List first few lines of a subroutine.
- List previous few lines.
b [line][cond] Set a breakpoint at line.
Example: b 233 $x > 10
b subname Set a (possible conditional) breakpoint at the
first line of named subroutine.

10
10/10/2013

Debugger Commands
 The Perl Debugger uses the following commands.
Debugger Commands
Command Description
d [line] Delete a breakpoint at the specified line.
D Delete all installed breakpoints.
p expr Print expr
x expr Evaluate its expression in a list context and
dump out the result.
s [expr] Single step.
n [expr] Next. Execute over subroutine calls, until the
beginning of the next statement.
r Return from current subroutine. OR run.
<CR> Repeat last “n” or “s” command.
c [line|sub] Continue, optionally insert a one-time breakpoint

Debugger Commands
 The Perl Debugger uses the following commands.
Debugger Commands
Command Description
/pattern/ Search forward for pattern.
?pattern? Search backward for pattern.
L [abw] List (default all) actions, breakpoints and watch
expressions.
S [! regex] List subroutine names.
T Produce a stack back trace.
a [line] cmd Set action to be done before the line is
executed.
A line Delete an action from the specified line.
w expr Add a global watch-expression.
W expr Delete watch-expression.

11
10/10/2013

Debugger Commands
 The Perl Debugger uses the following commands.
Debugger Commands
Command Description
! number Redo a previous command
H Display all previous commands
q OR ^D Quit
command Execute command as a Perl statement.
m expr List method to call on evaluating the
expression.
M Display all loaded modules and their versions.

Carp Module
 Useful in your own modules.

for die and warn functions from caller‟s


 Alternative
perspective.

 Helpful in run-time analysis.


 Functions
 carp( ) - Similar to warn in user module.
 croak( ) - Similar to die in user module.
 confess( ) – Gives function call history. Also called
stack trace.

12
10/10/2013

Devel::Peek Module
A data debugging tool for the XS programmer.
 Contains functions which allows raw Perl
datatypes to be manipulated from a Perl script.
 Used to check that the data they are sending from
C to Perl looks as they think it should look.
 Functions
 Dump( ) - dump a raw Perl data type.
 DumpArray( ) – allows to dump multiple values.
 mstat( ) – function to report memory usage.

13
10/10/2013

14

You might also like