| 1 | # -*- perl -*- | ||||||||||
| 2 | |||||||||||
| 3 | package Devel::Trace; | ||||||||||
| 4 | 1 | $VERSION = '0.12'; | |||||||||
| 5 | 1 | $TRACE = 1; | |||||||||
| 6 | |||||||||||
| 7 | # This is the important part. The rest is just fluff. | ||||||||||
| 8 | 0 | ▶ | sub DB::DB { | ||||||||
|
Subroutine
DB: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 9 | 0 | ▶ | return unless $TRACE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 10 | 0 | my ($p, $f, $l) = caller; | |||||||||
| 11 | 0 | my $code = \@{"::_<$f"}; | |||||||||
| 12 | 0 | print STDERR ">> $f:$l: $code->[$l]"; | |||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | sub import { | ||||||||
|
Subroutine
import: not called CC=3 SCAR=19.6
| |||||||||||
| 17 | 0 | my $package = shift; | |||||||||
| 18 | 0 | foreach (@_) { | |||||||||
| 19 | 0 | ▶ | if ($_ eq 'trace') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | my $caller = caller; | |||||||||
| 21 | 0 | *{$caller . '::trace'} = \&{$package . '::trace'}; | |||||||||
| 22 | } else { | ||||||||||
| 23 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 0 | croak "Package $package does not export `$_'; aborting"; | |||||||||
| 25 | } | ||||||||||
| 26 | } | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 1 | my %tracearg = ('on' => 1, 'off' => 0); | |||||||||
| 30 | 0 | ▶ | sub trace { | ||||||||
|
Subroutine
trace: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 31 | 0 | my $arg = shift; | |||||||||
| 32 | 0 | ▶ | $arg = $tracearg{$arg} while exists $tracearg{$arg}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | $TRACE = $arg; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 1 | 1; | |||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | =head1 NAME | ||||||||||
| 40 | |||||||||||
| 41 | Devel::Trace - Print out each line before it is executed (like C<sh -x>) | ||||||||||
| 42 | |||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||
| 44 | |||||||||||
| 45 | perl -d:Trace program | ||||||||||
| 46 | |||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||
| 48 | |||||||||||
| 49 | If you run your program with C<perl -d:Trace program>, this module | ||||||||||
| 50 | will print a message to standard error just before each line is executed. | ||||||||||
| 51 | For example, if your program looks like this: | ||||||||||
| 52 | |||||||||||
| 53 | #!/usr/bin/perl | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | print "Statement 1 at line 4\n"; | ||||||||||
| 57 | print "Statement 2 at line 5\n"; | ||||||||||
| 58 | print "Call to sub x returns ", &x(), " at line 6.\n"; | ||||||||||
| 59 | |||||||||||
| 60 | exit 0; | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | sub x { | ||||||||||
| 64 | print "In sub x at line 12.\n"; | ||||||||||
| 65 | return 13; | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | Then the C<Trace> output will look like this: | ||||||||||
| 69 | |||||||||||
| 70 | >> ./test:4: print "Statement 1 at line 4\n"; | ||||||||||
| 71 | >> ./test:5: print "Statement 2 at line 5\n"; | ||||||||||
| 72 | >> ./test:6: print "Call to sub x returns ", &x(), " at line 6.\n"; | ||||||||||
| 73 | >> ./test:12: print "In sub x at line 12.\n"; | ||||||||||
| 74 | >> ./test:13: return 13; | ||||||||||
| 75 | >> ./test:8: exit 0; | ||||||||||
| 76 | |||||||||||
| 77 | This is something like the shell's C<-x> option. | ||||||||||
| 78 | |||||||||||
| 79 | =head1 DETAILS | ||||||||||
| 80 | |||||||||||
| 81 | Inside your program, you can enable and disable tracing by doing | ||||||||||
| 82 | |||||||||||
| 83 | $Devel::Trace::TRACE = 1; # Enable | ||||||||||
| 84 | $Devel::Trace::TRACE = 0; # Disable | ||||||||||
| 85 | |||||||||||
| 86 | or | ||||||||||
| 87 | |||||||||||
| 88 | Devel::Trace::trace('on'); # Enable | ||||||||||
| 89 | Devel::Trace::trace('off'); # Disable | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | C<Devel::Trace> exports the C<trace> function if you ask it to: | ||||||||||
| 93 | |||||||||||
| 94 | import Devel::Trace 'trace'; | ||||||||||
| 95 | |||||||||||
| 96 | Then if you want you just say | ||||||||||
| 97 | |||||||||||
| 98 | trace 'on'; # Enable | ||||||||||
| 99 | trace 'off'; # Disable | ||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | =head1 TODO | ||||||||||
| 103 | |||||||||||
| 104 | =over 4 | ||||||||||
| 105 | |||||||||||
| 106 | =item * | ||||||||||
| 107 | |||||||||||
| 108 | You should be able to send the trace output to the filehandle of your choice. | ||||||||||
| 109 | |||||||||||
| 110 | =item * | ||||||||||
| 111 | |||||||||||
| 112 | You should be able to specify the format of the output. | ||||||||||
| 113 | |||||||||||
| 114 | =item * | ||||||||||
| 115 | |||||||||||
| 116 | You should be able to get the output into a string. | ||||||||||
| 117 | |||||||||||
| 118 | =back | ||||||||||
| 119 | |||||||||||
| 120 | We'll see. | ||||||||||
| 121 | |||||||||||
| 122 | =head1 LICENSE | ||||||||||
| 123 | |||||||||||
| 124 | Devel::Trace 0.11 and its source code are hereby placed in the public domain. | ||||||||||
| 125 | |||||||||||
| 126 | =head1 Author | ||||||||||
| 127 | |||||||||||
| 128 | =begin text | ||||||||||
| 129 | |||||||||||
| 130 | Mark-Jason Dominus (C<mjd-perl-trace@plover.com>), Plover Systems co. | ||||||||||
| 131 | |||||||||||
| 132 | See the C<Devel::Trace.pm> Page at http://www.plover.com/~mjd/perl/Trace | ||||||||||
| 133 | for news and upgrades. | ||||||||||
| 134 | |||||||||||
| 135 | =end text | ||||||||||
| 136 | |||||||||||
| 137 | =begin man | ||||||||||
| 138 | |||||||||||
| 139 | Mark-Jason Dominus (C<mjd-perl-trace@plover.com>), Plover Systems co. | ||||||||||
| 140 | |||||||||||
| 141 | See the C<Devel::Trace.pm> Page at http://www.plover.com/~mjd/perl/Trace | ||||||||||
| 142 | for news and upgrades. | ||||||||||
| 143 | |||||||||||
| 144 | =end man | ||||||||||
| 145 | |||||||||||
| 146 | =begin html | ||||||||||
| 147 | <p>Mark-Jason Dominus (<a href="mailto:mjd-perl-trace@plover.com"><tt>mjd-perl-trace@plover.com</tt></a>), Plover Systems co.</p> | ||||||||||
| 148 | <p>See <a href="http://www.plover.com/~mjd/perl/Trace/">The <tt>Devel::Trace.pm</tt> Page</a> for news and upgrades.</p> | ||||||||||
| 149 | |||||||||||
| 150 | =end html | ||||||||||
| 151 | |||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||