File Coverage

blib/lib/Devel/Quick.pm
Criterion Covered Total %
statement 143 152 94.0
branch 12 14 85.7
condition 6 9 66.6
subroutine 39 40 97.5
pod 0 1 0.0
total 200 216 92.5


line stmt bran cond sub pod time code
1             package Devel::Quick;
2             # ABSTRACT: Write single-step debugger one-liners easily (DB::DB)
3             $Devel::Quick::VERSION = '0.08';
4 1     1   952 use strict;
  1         3  
  1         38  
5 1     1   5 use warnings;
  1         2  
  1         511  
6              
7             sub import {
8 12     12   7897 my $class = shift;
9              
10 12         19 my $strict = 0;
11 12         13 my $begin = 0;
12              
13 12         22 my @opts = @_;
14              
15             # Parse leading options
16 12         22 for my $opt (@opts) {
17 21 100       63 if ($opt =~ /^-/) {
18 11 100 100     74 if ($opt eq '-s' || $opt eq '-strict') {
    100 100        
19 6         7 $strict = 1;
20 6         11 shift @_;
21             } elsif ($opt eq '-b' || $opt eq -'begin') {
22 3         4 $begin = 1;
23 3         6 shift @_;
24             } else {
25 2         12 require Carp;
26 2         37 Carp::croak("Unknown switch '$_[0]'");
27             }
28             } else {
29 10         18 last;
30             }
31             }
32              
33             # Put back in broken out commas...
34 10         23 my $code = join(",", @_);
35              
36 10         21 _gen_db_sub($code, $strict, $begin);
37             }
38              
39             sub _gen_db_sub {
40 10     10   16 my ($code, $strict, $begin) = @_;
41              
42 10         15 my $wrapper = <<'DBCODE';
43             package DB;
44              
45             use strict;
46             use warnings;
47              
48             <>
49              
50             sub DB {
51             # Get who called us
52             my ($package, $filename, $line) = caller(0);
53              
54             # Get the rest from the context of who called us
55             my (undef, undef, undef,
56             $subroutine, $hasargs, $wantarray,
57             $evaltext, $is_require, $hints,
58             $bitmask, $hinthash) = caller(1);
59              
60             return if $package && $package eq 'Devel::Quick';
61              
62             my $args = \@_;
63              
64             my $code;
65             {
66             no strict 'refs';
67             $code = @{"::_<$filename"}[$line];
68             }
69              
70             <>
71              
72             <>
73             }
74              
75             1;
76             DBCODE
77              
78             # Leave strict enabled if explicitly asked for
79 10 100       19 if ($strict) {
80 5         25 $wrapper =~ s/<>//;
81             } else {
82 5         25 $wrapper =~ s/<>/no strict;/;
83             }
84              
85             # Are we stepping as soon as possible?
86 10 100       21 if ($begin) {
87 2         9 $wrapper =~ s/<>/\$DB\::single = 1;/;
88             } else {
89 8         27 $wrapper =~ s/<>//;
90             }
91              
92 10         38 $wrapper =~ s/<>/$code/;
93              
94 1 0 0 1 0 8 eval $wrapper;
  1     1   1  
  1     1   27  
  1     1   5  
  1     1   1  
  1     1   205  
  1     1   5  
  1     1   2  
  1     1   41  
  1     1   5  
  1     1   3  
  1     1   38  
  1     1   8  
  1     1   2  
  1     1   34  
  1     1   6  
  1     1   2  
  1     1   136  
  1     1   5  
  1     1   2  
  1     1   52  
  1     1   6  
  1     1   2  
  1     1   40  
  1     1   6  
  1     1   2  
  1     1   27  
  1     1   4  
  1     1   3  
  1     1   137  
  1     1   7  
  1     1   2  
  1     1   84  
  1     1   27  
  1     1   2  
  1     0   30  
  1         4  
  1         3  
  1         123  
  1         5  
  1         3  
  1         60  
  1         7  
  1         2  
  1         36  
  1         5  
  1         1  
  1         138  
  1         5  
  1         2  
  1         69  
  1         5  
  1         3  
  1         31  
  1         5  
  1         3  
  1         122  
  1         647  
  1         2  
  1         46  
  1         5  
  1         2  
  1         47  
  1         5  
  1         1  
  1         26  
  1         5  
  1         2  
  1         104  
  1         4  
  1         2  
  1         59  
  1         6  
  1         2  
  1         27  
  1         5  
  1         1  
  1         211  
  1         6  
  1         2  
  1         74  
  1         5  
  1         2  
  1         38  
  1         5  
  1         1  
  1         135  
  1         6  
  1         2  
  1         50  
  1         5  
  1         2  
  1         51  
  1         6  
  1         2  
  1         29  
  1         5  
  1         1  
  1         158  
  1         5  
  1         2  
  1         49  
  1         6  
  1         1  
  1         53  
  10         1721  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
95              
96 10 100       72 if (my $err = $@) {
97             # Add in line numbers
98 3         5 my $i = 1;
99 3         27 $wrapper =~ s/(^|\n)/sprintf("\n%3d:\t", $i++)/ge;
  102         575  
100              
101 3         16 require Carp;
102 3         60 Carp::croak("Failed to parse code: $err; code:\n$wrapper");
103             }
104             }
105              
106             1;
107              
108             =head1 NAME
109              
110             Devel::Quick - Write single-step debugger one-liners easily (DB::DB)
111              
112             =head1 VERSION
113              
114             version 0.08
115              
116             =head1 SYNOPSIS
117              
118             Devel::Trace in one line:
119              
120             perl -d:Quick='print ">> $filename:$line $code"' prog.pl
121              
122             The above, with L checking enabled (not default):
123              
124             perl -d:Quick=-strict,'print ">> $filename:$line $code"' prog.pl
125              
126             Or shortened:
127              
128             perl -d:Quick=-s,'print ">> $filename:$line $code"' prog.pl
129              
130             The above, but start stepping immediately (look at code in "use ..."
131             statements)
132              
133             perl -d:Quick=-begin,'print ">> $filename:$line $code"' prog.pl
134              
135             Or shortened:
136              
137             perl -d:Quick=-b,'print ">> $filename:$line $code"' prog.pl
138              
139             You can combine opts:
140              
141             perl -d:Quick=-s,-b,'print ">> $filename:$line $code"' prog.pl
142              
143             If you need '-' as the first character in your code, use a ';':
144              
145             perl -d:Quick='; -1 * 2;' prog.pl
146              
147             =head1 DESCRIPTION
148              
149             This module allows you to write simple on-the-fly C line debuggers
150             easily. It injects the following code around the code passed to its import
151             method and eval's it in:
152              
153             package DB;
154              
155             use strict;
156             use warnings;
157              
158             $DB::single = 1;
159              
160             sub DB {
161             # Get who called us
162             my ($package, $filename, $line) = caller(0);
163            
164             # Get the rest from the context of who called us
165             my (undef, undef, undef,
166             $subroutine, $hasargs, $wantarray,
167             $evaltext, $is_require, $hints,
168             $bitmask, $hinthash) = caller(1);
169            
170             return if $package && $package eq 'Devel::Quick';
171              
172             my $args = \@_;
173            
174             my $code;
175             {
176             no strict 'refs';
177             $code = @{"::_<$filename"}[$line];
178             }
179              
180             no strict;
181            
182             <>
183             }
184              
185             By default, warnings are enabled but strict mode is disabled. If you want
186             strict, the first argument to import should be C<-s> or C<-strict>.
187              
188             By default, tracing also starts after compile time. This means that code in
189             use statements will not be seen. If you want to trace into use statements,
190             use the C<-b> or C<-begin> flag.
191              
192             If you need to pass a C<-> as the first character in the Perl code, you'll need
193             to inject a semi-colon (;) before it like so:
194              
195             perl -d:Quick='; -1 * 2;' prog.pl
196              
197             =head2 Available Arguments
198              
199             A bunch of varibales are provided by default for ease of use, including all
200             variables returned by L, the source code that's about to be
201             executed, and arguments to a subroutine if the code being executed is from one. All
202             described below.
203              
204             =head3 caller() variables
205              
206             See L for a description of these.
207              
208             =over 4
209              
210             =item * B<$package>
211              
212             =item * B<$filename>
213              
214             =item * B<$line>
215              
216             =item * B<$subroutine>
217              
218             =item * B<$hasargs>
219              
220             =item * B<$wantarray>
221              
222             =item * B<$evaltext>
223              
224             =item * B<$is_require>
225              
226             =item * B<$hints>
227              
228             =item * B<$bitmask>
229              
230             =item * B<$hinthash>
231              
232             =back
233              
234             =head3 $code
235              
236             The variable B<$code> contains the line of source code about to be executed.
237             This is provided by C<< @{"_<$filename"} >>. See L for more
238             information.
239              
240             =head3 $args
241              
242             B<$args> is simply a reference to C<@_> that the code that is about to be
243             executed can see. This is only relevant within subroutines. B<$hasargs> may tell
244             you if this is filled in or not, or just check @$args.
245              
246             Changing the underlying values will affect what the current subroutine sees.
247              
248             =head1 AUTHOR
249              
250             Matthew Horsfall (alh) -
251              
252             =cut
253              
254             1;