in reply to Re: Monkey patching all subs with a basic 'starting subname' message
in thread Monkey patching all subs with a basic 'starting subname' message
I responded on that thread, so I'll go another round.
When I first started with Perl, I was dropped into a situation where I had a less-than-sane codebase. The very first thing I desired to design was something that could tell me stuff about code. I got familiar with the tools others have mentioned, but still wanted to figure it out.
Devel::Trace::Subs was the creation of that, after having to go on another huge tangent with Devel::Examine::Subs as its backbone.
This is intrusive; it literally writes to library files:
perl -MDevel::Trace::Subs=install_trace -e 'install_trace(file => "Dat +a::Dump")'
At this point, you've infected your ../Data/Dump.pm file. This is a global example, you can fine-tune what you're tracing. So a script that looked like this:
use warnings; use strict; use Data::Dump qw(dd); three(); sub one { return {a => 1, b => 2}; } sub two { return one(); } sub three { dd two(); }
That output this:
{ a => 1, b => 2 }
...now looks like this:
use warnings; use strict; use Data::Dump qw(dd); use Devel::Trace::Subs qw(trace_dump); $ENV{DTS_ENABLE} = 1; three(); trace_dump(); sub one { return {a => 1, b => 2}; } sub two { return one(); } sub three { dd two(); }
... and outputs:
{ a => 1, b => 2 } Code flow: 1: Data::Dump::dd 2: Data::Dump::dump 3: Data::Dump::_dump 4: Data::Dump::tied_str 5: Data::Dump::_dump 6: Data::Dump::_dump 7: Data::Dump::format_list Stack trace: in: Data::Dump::dd sub: main::three file: script.pl line: 20 package: main in: Data::Dump::dump sub: Data::Dump::dd file: /usr/local/share/perl/5.22.1/Data/Dump.pm line: 84 package: Data::Dump in: Data::Dump::_dump sub: Data::Dump::dump file: /usr/local/share/perl/5.22.1/Data/Dump.pm line: 36 package: Data::Dump in: Data::Dump::tied_str sub: Data::Dump::_dump file: /usr/local/share/perl/5.22.1/Data/Dump.pm line: 292 package: Data::Dump in: Data::Dump::_dump sub: Data::Dump::_dump file: /usr/local/share/perl/5.22.1/Data/Dump.pm line: 331 package: Data::Dump in: Data::Dump::_dump sub: Data::Dump::_dump file: /usr/local/share/perl/5.22.1/Data/Dump.pm line: 331 package: Data::Dump in: Data::Dump::format_list sub: Data::Dump::dump file: /usr/local/share/perl/5.22.1/Data/Dump.pm line: 65 package: Data::Dump
You can then just revert your changes:
perl -MDevel::Trace::Subs=remove_trace -e 'remove_trace(file => "Data: +:Dump")'
The output is kind of configurable in that there are two very basic templates with the ability to send in an html flag for rendering. You can also limit the output to flow or trace.
There are many other tools that do this kind of thing that are far better and far more widespread, and unless one is brave, they'd use a non-intrusive proper profiler or something that's been through the ropes. I just like how OP is proud of their __dbg() hijacker, so I thought I'd share :)
update: Mentioned Devel::NYTProf as a profiler, but if OP wants to get into examining files/code itself, invest in learning some PPI.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Monkey patching all subs with a basic 'starting subname' message
by Anonymous Monk on Jul 17, 2017 at 21:48 UTC |