ropey has asked for the wisdom of the Perl Monks concerning the following question:

I am stumped !!!

I have unfortunately inherited a rather nasty beast of object orientated code, the sort where object orientation was used *because you could* not because you *should*. The problem is evertime I try and fix a problem I look for the correct sub and there and behold is SUPER::sub_name, so I look up the inheritance tree and find the next level also has SUPER::sub_name. So to cut a long story short it takes forever to solve the simplest of problems. Now what I need to do is somehow follow what is called by what, I though CGI::Carp would give me some sort of stacktrace but it seems to get confused. So does anyone know a method how I can do a stacktrace, i.e. at any given point I can do a carp and it backtracks to the original sub call. I hope this makes sense !!!

Thanks in anticipation

Replies are listed 'Best First'.
Re: Subroutine Tracing
by tachyon (Chancellor) on Dec 10, 2002 at 20:55 UTC

    What you need is a confession.....

    use Carp; hello(); sub hello { again() } sub again { my_friend() } sub my_friend { I(); } sub I { called(); } sub called { to_say() } sub to_say { confess('Goodbye, I died'); } __DATA__ Goodbye, I died at script line 26 main::to_say() called at script line 23 main::called() called at script line 18 main::I() called at script line 14 main::my_friend() called at script line 11 main::again() called at script line 7 main::hello() called at script line 3

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Subroutine Tracing
by particle (Vicar) on Dec 10, 2002 at 21:14 UTC
Re: Subroutine Tracing
by cLive ;-) (Prior) on Dec 10, 2002 at 23:34 UTC
    caller can be your friend too...
    sub calltrace { my $i=-1; print "Caller Trace:\n\n"; while ( caller(++$i) ) { printf("-> %s at line %s\n", (caller($i))[1,2]); } } exit(0); }

    call sub for a call trace to that point.

    .02

    cLive ;-)

Re: Subroutine Tracing
by graff (Chancellor) on Dec 10, 2002 at 20:54 UTC
    Maybe it would be helpful to index the source code you have, in such a way that you can see where all the sub definitions really are -- in other words, something along the lines of "grep -n sub_name *.pl" (assuming all the source code files for this set of object modules is in the current directory), but more comprehensive than that. There is a simple-minded first attempt here, which might at least be suggestive. You could probably adapt this sort of approach by flagging the cases of "sub sub_name {...}" that happen to contain "SUPER::sub_name", so as to eliminate all the false alarms.

    (A real stack trace would of course be very useful as well, giving information that this sort of index cannot provide, but -- sorry -- I haven't had much exposure to that issue.)