in reply to Searching for duplication in legacy code

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:

use warnings; use strict; use Devel::Trace::Subs qw(trace_dump); use Mock::Sub; $ENV{DTS_ENABLE} = 1; my $mock = Mock::Sub->new; my $blah_sub = $mock->mock('blah'); blah(); trace_dump(); sub blah { print "blah!\n"; }

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():

Code flow: 1: Mock::Sub::new 2: Mock::Sub::mock 3: Mock::Sub::Child::new 4: Mock::Sub::Child::side_effect 5: Mock::Sub::Child::_check_side_effect 6: Mock::Sub::Child::return_value 7: Mock::Sub::Child::_mock 8: Mock::Sub::Child::name 9: Mock::Sub::Child::_check_side_effect 10: Mock::Sub::Child::__ANON__ Stack trace: in: Mock::Sub::new sub: - file: line: 10 package: main in: Mock::Sub::mock sub: - file: line: 11 package: main in: Mock::Sub::Child::new sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/ line: 50 package: Mock::Sub in: Mock::Sub::Child::side_effect sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/ line: 52 package: Mock::Sub in: Mock::Sub::Child::_check_side_effect sub: Mock::Sub::Child::side_effect file: /usr/local/share/perl/5.18.2/Mock/Sub/ line: 185 package: Mock::Sub::Child in: Mock::Sub::Child::return_value sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/ line: 53 package: Mock::Sub in: Mock::Sub::Child::_mock sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/ line: 56 package: Mock::Sub in: Mock::Sub::Child::name sub: Mock::Sub::Child::_mock file: /usr/local/share/perl/5.18.2/Mock/Sub/ line: 49 package: Mock::Sub::Child in: Mock::Sub::Child::_check_side_effect sub: Mock::Sub::Child::_mock file: /usr/local/share/perl/5.18.2/Mock/Sub/ line: 81 package: Mock::Sub::Child in: Mock::Sub::Child::__ANON__ sub: - file: line: 13 package: main

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.