blib/lib/Devel/TraceVars.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 87 CC 9 SCAR 29.6
CC
9
Cov
0%
import
42.0
evaluate
12.0
DB::DB
6.0
CRAP 19.4
1 =head1 NAME
2  
3 Devel::TraceVars - Print each line of code with variables evaluated
4  
5 =head1 SYNOPSIS
6  
7 $ perl -d:TraceVars[+MODE[OPTIONS]] program [arguments]
8  
9 Where MODE can be C<modules> or C<custom>.
10  
11 If C<modules> mode is selected then the output will only include the lines
12 that are provided by the given modules.
13  
14 Modules can be added by separating them by commas C<,>. By default the module
15 C<main> is assumed to be included. Optionally, the wildcard C<*> can be used
16 to indicate any module that matches the given pattern. Thus, the entry
17 C<Net::SSH::*> will match all modules that start with Net::SSH.
18  
19 B<NOTE:> The wild card handling works only if it's used as the last character.
20 Using the following pattern I<Net::*::Perl> isn't supported yet.
21  
22 If C<non-cpan> is used then output will exclude all files that are provided by
23 CPAN. At present, a module is considered to be provided by CPAN if the module
24 is loaded from the Perl include path as designated by $Config{installarchlib}.
25  
26 If no mode is specified then all output lines are printed.
27  
28 Examples:
29  
30 C<perl -d:TraceVars script.pl>
31  
32 Prints all lines.
33  
34 C<perl -d:TraceVars+modules script.pl>
35  
36 Defaults to print only all lines of the package C<main> which usually means all
37 lines in the main program. The package C<main> is the default package used by
38 perl when no package is specified in the code.
39  
40 C<perl -d:TraceVars+modules,Net::SSH,Digest::* script.pl>
41  
42 Prints only information from the module C<Net::SSH>, and from any module whose
43 name starts with C<Digest>.
44  
45 C<perl -d:TraceVars+noncpan script.pl>
46  
47 Prints only lines that are provided by perl files that aren't stored in the
48 default folder where all CPAN modules are installed.
49  
50 =head1 DESCRIPTION
51  
52 If you run your program with C<perl -d:TraceVars program>, this module
53 will print the current line of code being executed to standard error
54 just before each line is executed. The contents of all scalar variables will
55 be evaluated and displayed as well. All leading and trailing spaces will be
56 removed from each line.
57  
58 =cut
59  
60 package Devel::TraceVars;
61  
62 use strict;
63 use Data::Dumper;
64 use PadWalker qw(peek_my);
65 use Config;
66  
67  
68 ##
69 # Variables
70 ##
71  
72 our $VERSION = '0.03';
73  
74  
75 #
76 #
77 #This is the main closure used to include/exclude modules from the output.
78 #
79 #This closure will receive as arguments:
80 # $package
81 # $filename
82 # $line
83 #
84 my $ACCEPT_CLOSURE = sub { return 1; };
85  
86  
87 ##
88 # Methods
89 ##
90  
91  
92 =head1 METHODS
93  
94 The following methods are available.
95  
96 =cut
97  
98  
99 =head2 import
100  
101 Called by perl when this module is first loaded.
102  
103 =cut
104  
105 sub import {
106 my $module = shift;
107
108 # Stop if there are no arguments
109 return unless @_;
110
111 # The execution mode
112 my $mode = lc shift;
113
114 # Check which Execution mode is requested
115 if ($mode eq 'modules') {
116
117 # Modules to accept
118 my %modules = map { $_ => 1 } ('main', @_);
119
120 $ACCEPT_CLOSURE = sub {
121 my ($package, $filename, $line) = @_;
122  
123 # Keep only if module is in the list of known modules
124 return 1 if $modules{$package};
125
126 # Try to see if the module starts with one of the accepted prefixes
127 my @path = split /::/, $package;
128 while (@path) {
129 pop @path;
130 my $new_package = join '::', @path, '*';
131 return 1 if $modules{$new_package};
132 }
133
134 # Not found
135 return 0;
136 }
137 }
138 elsif ($mode eq 'noncpan') {
139 my $regexp = qr/(\(eval \d+\)\[)?\Q$Config{installarchlib}\E/;
140 $ACCEPT_CLOSURE = sub {
141 my ($package, $filename, $line) = @_;
142  
143 # Skip if dealing with a perl stock module
144 return $filename !~ /^$regexp/;
145 }
146 }
147 }
148  
149  
150 =head2 DB::DB
151  
152 This is the important part. Perl calls this method before each line is executed.
153  
154 =cut
155  
156 sub DB::DB {
157
158 # Information about the caller
159 my ($caller_package, $caller_filename, $caller_line) = caller;
160  
161 # Skip if dealing with a perl stock module
162 return unless $ACCEPT_CLOSURE->($caller_package, $caller_filename, $caller_line);
163
164 my $code_line;
165 {
166 no strict 'vars';
167 # Get the typeglob that holds a handle to the code being executed
168 local *symbol = $main::{"_<" . $caller_filename};
169 $code_line = $symbol[$caller_line];
170 chomp ($code_line);
171 }
172
173  
174 # Evaluate all variables in the code
175 my $evaluated_code_line = $code_line;
176 $evaluated_code_line =~ s/\$(\w+)/evaluate($caller_package, $1)/ge;
177
178 # Trim spaces
179 foreach my $alias ($code_line, $evaluated_code_line) {
180 $alias =~ s/(^\s+|\s+$)//g;
181 }
182
183 # Output the debug line
184 print STDERR ">> $caller_filename $caller_line: $evaluated_code_line\n";
185 }
186  
187  
188 =head2 evaluate($package, $variable)
189  
190 Tries to evaluate the content of the given variable.
191  
192 =over 4
193  
194 =item * $package
195  
196 The package's scope where the variable was used.
197  
198 =item * $variable
199  
200 The variable name to evaluate.
201  
202 =back
203  
204 Returns the variable's content.
205  
206 =cut
207  
208 sub evaluate {
209
210 my ($package, $variable) = @_;
211  
212 # Causes segmentation faults some times
213 return '$VERSION' if $variable eq 'VERSION';
214
215 ##
216 # Check if the variable is a my variable
217
218 # Since this is also a function we need to peek two levels up
219 my $peek = peek_my(2);
220
221 # See if there is a my variable declared
222 my $variable_to_evaluate = sprintf '$%s', $variable;
223 if (defined $peek->{$variable_to_evaluate}) {
224 my $value = ${ $peek->{$variable_to_evaluate} };
225 return $value;
226 }
227
228
229 ##
230 # Try a package variable
231 # The code to evaluate $PACKAGE::VARIABLE;
232 $variable_to_evaluate = sprintf '$%s::%s', $package, $variable;
233  
234 # Perform the Evaluation
235 # if the variable is not defined yet (probably where are evaluting the declaration)
236 # then the variable will be replaced by it's own name including the $
237 my $value = eval "defined $variable_to_evaluate ? $variable_to_evaluate : '\$$variable'";
238  
239 return $value;
240 }
241  
242  
243 # Return a true value
244 1;
245  
246  
247 =head1 BUGS
248  
249 Probably the code won't work with Perl's special variables.
250 Sometimes perl seems to core dump when using this module.
251  
252 =head1 SEE ALSO
253  
254 L<Devel::Trace>.
255  
256 =head1 AUTHOR
257  
258 Emmanuel Rodriguez E<lt>potyl@cpan.orgE<gt> based on a shameless copy of
259 C<Devel::Trace> .
260  
261 =cut