Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: 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:

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.

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2023-03-23 21:08 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.