Devel-DumpTrace
view release on metacpan or search on metacpan
lib/Devel/DumpTrace.pm view on Meta::CPAN
# a demonstration of Devel::DumpTrace
$a = 1;
$b = 3;
$c = 2 * $a + 7 * $b;
@d = ($a, $b, $c + $b);
then the C<DumpTrace> output will look like:
$ perl -d:DumpTrace demo.pl
>>>>> demo.pl:3: $a:1 = 1;
>>>>> demo.pl:4: $b:3 = 3;
>>>>> demo.pl:5: $c:23 = 2 * $a:1 + 7 * $b:3;
>>>>> demo.pl:6: @d:(1,3,26) = ($a:1, $b:3, $c:23 + $b:3);
There are also more I<verbose> modes which will produce even more
detailed output:
$ perl -d:DumpTrace=verbose demo.pl
>> demo.pl:3:
>>> $a = 1;
>>>>> 1 = 1;
-------------------------------------------
>> demo.pl:4:
>>> $b = 3;
>>>>> 3 = 3;
-------------------------------------------
>> demo.pl:5:
>>> $c = 2 * $a + 7 * $b;
>>>> $c = 2 * 1 + 7 * 3;
>>>>> 23 = 2 * 1 + 7 * 3;
-------------------------------------------
>> demo.pl:6:
>>> @d = ($a, $b, $c + $b);
>>>> @d = (1, 3, 23 + 3);
>>>>> (1,3,26) = (1, 3, 23 + 3);
-------------------------------------------
See C<$Devel::DumpTrace::TRACE> under the L</"VARIABLES"> section
for more details about the different levels of verbosity.
This distribution comes with both a basic parser and a
L<PPI-based parser|Devel::DumpTrace::PPI> (which relies on L<PPI>
to understand your source code). The PPI version has more features
and fewer limitations than the basic parser. If the L<PPI|PPI>
module is installed on your system, then this module will automatically
use the PPI-based parser to analyze the traced code. You can
force this module to use the basic parser by running with the
C<-d:DumpTrace::noPPI> argument or by setting the C<DUMPTRACE_NOPPI>
environment variable:
# use PPI if available, otherwise use basic parser
$ perl -d:DumpTrace program.pl
# use PPI, fail if it is not available
$ perl -d:DumpTrace::PPI program.pl
# always uses basic parser
$ perl -d:DumpTrace::noPPI program.pl
$ DUMPTRACE_NOPPI=1 perl -d:DumpTrace program.pl
See the L</"BUGS AND LIMITATIONS"> section for important, er, limitations
of this module, especially for the basic parser.
=head1 SUBROUTINES/METHODS
None of interest.
=head1 VARIABLES
=head2 $TRACE
=head2 C<$Devel::DumpTrace::TRACE>
Controls whether and how much output is produced by this module.
Setting C<$Devel::DumpTrace::TRACE> to zero will disable the module.
Since this module can produce a lot of output and has other overhead
that can considerably slow down your program
(by a factor of 50 or more), you may find it
useful to toggle this variable for critical sections of your code
rather than leave it set for the entire program. For example:
BEGIN { $Devel::DumpTrace::TRACE = 0 }
&some_non_critical_code_that_more_or_less_works();
$Devel::DumpTrace::TRACE = 'normal';
&the_critial_code_you_want_to_debug();
$Devel::DumpTrace::TRACE = 0;
&some_more_non_critical_code();
or to enable tracing in a C<local> block:
{
local $Devel::DumpTrace::TRACE = 1;
&the_critical_code;
}
In general higher values of C<$Devel::DumpTrace::TRACE> will cause
more output to be produced.
Let's consider this simple program to see how the different
C<$Devel::DumpTrace::TRACE> settings affect the output:
@a = (1 .. 40);
$b = $a[4];
=over 4
=item C<$Devel::DumpTrace::TRACE> == 1
is the quietest mode. One line of output for each statement evaluated.
The name of each variable in the source code and its value are included
in the same line of output. Values of long scalars, long arrays, or
long hash tables are heavily abbreviated:
$ perl -d:DumpTrace=1 littledemo.pl
>>>>> littledemo.pl:1:[__top__]: @a:(1,2,3,4,5,6,...,40) = (1 .. 40);
>>>>> littledemo.pl:2:[__top__]: $b:5 = $a:(1,2,3,4,5,6,...,40)[4];
=item C<$Devel::DumpTrace::TRACE> == 2
lib/Devel/DumpTrace.pm view on Meta::CPAN
is executed.
If the C<DUMPTRACE_COUNT> environment variable is set to a true value,
this module will include a count with the file and line number in all
trace output, indicating how many times your program has visited a
particular line of code.
The default behaviour of C<Devel::DumpTrace> is to include the name of
the current subroutine each time the file and line number are displayed.
If C<DUMPTRACE_NO_SUB> environment variable is set to a true value,
then the subroutine name will not be displayed.
C<DUMPTRACE_TIME>, C<DUMPTRACE_PID>, C<DUMPTRACE_COUNT>, and
C<DUMPTRACE_NO_SUB> may be used separately or in any combination.
When more than one environment variable needs to be set, the caller
can use the C<DUMPTRACE> environment variable to set multiple variables
concisely. If C<$ENV{DUMPTRACE}> is set, this module will split
the variable value into key value pairs and update the other relevant
environment variables. That is,
DUMPTRACE=PID=1,FH=trace.out,EXCLPKG=My::Module
is equivalent to the longer
DUMPTRACE_PID=1 DUMPTRACE_FH=trace.out DUMPTRACE_EXCL=My::Module
If C<DUMPTRACE_COLOR> is set, and if the L<Term::ANSIColor|Term::ANSIColor>
module can be loaded, then C<Devel::DumpTrace> output will be colored in
the specified color. If your program produces output and you are writing
C<Devel::DumpTrace> output to your console, the different color of the
DumpTrace output will help the actual output from the program stand out.
Example:
DUMPTRACE_COLOR="bold yellow on_black" perl -d:DumpTrace myScript.pl
=cut
Documented in Devel/DumpTrace/PPI.pm: $ENV{DUMPTRACE_DUMB_ABBREV}
=head1 INCOMPATIBILITIES
None known.
=head1 EXPORT
Nothing is exported from this module.
=head1 DIAGNOSTICS
All output from this module is for diagnostics.
=head1 DEPENDENCIES
L<PadWalker|PadWalker> for arbitrary access to lexical variables.
L<Scalar::Util|Scalar::Util> for the reference identification
convenience methods.
=head1 BUGS AND LIMITATIONS
=head2 Parser limitations
Some known cases where the output of this module will
be incorrect or misleading include:
=head3 Multiple statements on one line
$b = 7;
$a=4; $b=++$a;
=================================
>>>>> 4=4; 7=++undef;
>>>>> 5=4; 7=++4;
All expressions on a line are evaluated, not just expressions in the statement
currently being executed. Also see the basic parser limitation below concerning
multiple lines for one statement.
=head3 Statements with chained assignments; complex assignment expressions
($a,$b) = ('','bar');
$a = $b = 'foo';
>>>>> 'foo' = 'bar' = 'foo';
$rin=$ein=3;
>> select $rout=$in,undef,$eout=$ein,0;
>>> select $rout=3,undef,undef=3,0;
>>>>> select 3=3,undef,undef=3,0;
Everything to the right of the I<first> assignment operator in a
statement is evaluated I<before> the statement is executed.
=head3 Displayed value of @_ variable is unreliable
The displayed value of C<@_> inside a subroutine is subject to
some of the issues described in L<perlfunc/"caller">:
... be aware that setting @DB::args is best effort, intended for
debugging or generating backtraces, and should not be relied upon
... a side effect of the current implementation means that effects
of shift @_ can normally be undone (but not pop @_ or other splicing,
and not if a reference to @_ has been taken, and subject to the caveat
about reallocated elements), so @DB::args is actually a hybrid of the
current state and initial state of @_ . Buyer beware.
That is, the displayed value of C<@_> inside a subroutine may be
corrupted. Different versions of Perl may have different behavior.
=head3 C<grep EXPR,LIST> and C<map EXPR,LIST> statements
C<grep EXPR,LIST> and C<map EXPR,LIST> constructions are evaluated
a single time, after the entier C<LIST> has been evaluated, and this
module does not let you drill down to how each element of the list
was evaluated with the given C<EXPR>. The constructions
C<grep BLOCK LIST> and C<map BLOCK LIST>, however, will display the
C<BLOCK> evaluation for each element of the C<LIST>.
=head2 Basic parser limitations
( run in 2.302 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )