Perl: the Markov chain saw | |
PerlMonks |
Re: Searching for duplication in legacy codeby stevieb (Canon) |
on Nov 23, 2016 at 21:44 UTC ( [id://1176454]=note: print w/replies, xml ) | Need Help?? |
Perhaps another thing that can help you sort out what is calling each sub from where is to enable stack tracing in all of your subs. Normally I wouldn't go so far away from the original question, but looking at my module this morning had me testing a few others so I thought I'd throw it out there in hopes it can help in some way. I wrote Devel::Trace::Subs to do this tracing. It uses Devel::Examine::Subs in the background (in fact, I wrote Devel::Examine::Subs originally specifically to be used by this module). It is intrusive... it injects a command into every single sub within specified files (both inserting and removing is done with a single command line string). Here's an example where I configure every Perl file in my Mock::Sub directory to save trace information (there's only one file in this case, but I still just use the current working directory as the 'file' param. Configure all files (make a backup copy of your directory first!): perl -MDevel::Trace::Subs=install_trace -e 'install_trace(file => ".");'In my case, I install my distribution, but that may not be your case if scripts just access the libraries where they sit. Here's an example script that uses the module that now has tracing capabilities:
The only parts of interest are the use Devel::Trace::Subs ... line, the $ENV{DTS_ENABLE} =1; line which enables the tracing, and the trace_dump(); line which dumps the trace data. The Mock::Sub stuff and everything else is irrelevant, it's just an example of normal code flow using other modules. Here is the output of the trace_dump():
in: is the sub currently being executed. The rest of the info is the caller of that sub. After you're done, you can remove tracing just as easily: perl -MDevel::Trace::Subs=remove_trace -e 'remove_trace(file => ".");'In the above example, there's only a single library. If the directory had several, you'd see the calls between the different modules in the proper order.
In Section
Seekers of Perl Wisdom
|
|