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: mock.pl line: 10 package: main in: Mock::Sub::mock sub: - file: mock.pl line: 11 package: main in: Mock::Sub::Child::new sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/Sub.pm line: 50 package: Mock::Sub in: Mock::Sub::Child::side_effect sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/Sub.pm 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/Child.pm 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/Sub.pm line: 53 package: Mock::Sub in: Mock::Sub::Child::_mock sub: Mock::Sub::mock file: /usr/local/share/perl/5.18.2/Mock/Sub.pm 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/Child.pm 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/Child.pm line: 81 package: Mock::Sub::Child in: Mock::Sub::Child::__ANON__ sub: - file: mock.pl 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.


In reply to Re: Searching for duplication in legacy code by stevieb
in thread Searching for duplication in legacy code by yulivee07

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.