Listing perl code

Using the perl debugger to produce a code listing.

l usage
l [min+incr|min-max|line|subname|$var]

List code within the currently loaded file, in “windowSize” chunks. Note
you can always get back to the current code position with the “.” command
(below).

See options for more information about “windowSize”

opx-p1200794Sometimes the default “windowSize” view of your code just is not
sufficient, and knowing how to modify the view can make the difference
between an easy trouble-shooting session and a hard one.

l
List code from current viewing position

perldb@monkey> B<perl -d linecounter.pl>
<…truncated output…>
main::(linecounter.pl:8): my @args = @ARGV;
DB<1> B<l>
8==> my @args = @ARGV;
9
10 # help requested?
11: my $help = grep(/^-h(elp)*$/i, @args);
12: if ($help) {
13: logg(help());
14: exit 0;
15 }
16
17 # get the pattern to match against.
DB<1>

Now do it again to continue the listing:

C<>
DB<1> B<l>
18: my $REGEX = shift @args || ”;
19
20 # get the files for processing.

21: my @files = grep(!/^-h(elp)*$/i, @args);

22: unless (@files) {

23: push(@files, $0);
24: logg(“using default $0 while no files given”);
25 }
26
27 # loop through the files
DB<1>

l line
List the single line of code at line 11.

C<>
DB<1> B<l 11>
11: my $help = grep(/^-h(elp)*$/i, @args);
DB<2>

l min+incr
List the range of code from line number min and the following incr lines.

C<>
DB<2> B<l 11+3>
11: my $help = grep(/^-h(elp)*$/i, @args);
12: if ($help) {
13: logg(help());
14: exit 0;
DB<3>

l min-max
List the range of code from line number min up to and including line
number max.

C<>
DB<3> B<l 11-14>
11: my $help = grep(/^-h(elp)*$/i, @args);
12: if ($help) {
13: logg(help());
14: exit 0;
DB<4>

l subname
List “windowSize” lines of code of the given subroutine.

C<>
DB<4> B<l report>
56 sub report {
57: my $FH = shift;
58: my $regex = shift;
59: my %report = ();
60: my $i_cnt = 0;
61: while (<$FH>) {
62: $i_cnt++;
63: my $i_match = 0;
64: my $line = $_;
65: if ($line =~ /($regex)/) {
DB<5>

The subroutine may be in any file loaded in %INC.

C<>
DB<5> B<l Carp::croak>
Switching to file ‘/usr/lib/perl5/5.8.0/Carp.pm’.
191: sub croak { die shortmess @_ }
DB<6>

 

Extract from the Perl Debugger Pocket Reference book.