3

I am new to Perl and I have the following problem.
I have a log output and I have found where this log output comes from. I mean the subroutine in some module that prints it.

Now e.g. in Java via Eclipse I would use e.g. Call hierarchy and other utilities to see how/when/who calls the method and figure out how to reproduce what I need and debug.

How can I do this in Perl? Via e.g. grep? If I grep e.g. for the module name I get hundrends of lines ranging from use A require A C::B::A B::A C::B::A::some_routine C::B::A::some_other_routine etc.
On top of this I am worried that perhaps the routine I am interested in is not called directly but some script e.g. runs the module that is of interest to me via some obscure (to me due to my ignorance in Perl) manner.

So how would I go debug something in Perl in the most efficient way? What do you Perl gurus suggest for me to do and become more efficient?

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • If you have the EPIC IDE for Eclipse then you can set a breakpoint in the subroutine in the usual way. You can see the call stack from there once the program is paused. – Borodin May 25 '13 at 20:28

3 Answers3

5

Run the program under the Perl debugger:

perl -d scriptname arguments...

Set a breakpoint in the function you care about, and when the program stops at the breakpoint use the T debugger command to display a stack trace, which will show where the function was called from.

From your comments, I'm not sure this actually addresses what you're looking for. Maybe what you want is a cross-reference of the Perl application? See the FAQ How do I cross-reference my Perl programs?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • +1. I don't know where the program starts. I mean there is no `main` like in `Java` or `C++`. From my (ignorant) perspective I have many scripts that could call the module and I don't really know what the flow is.Does what I am saying make sense? – Cratylus May 25 '13 at 19:08
  • Perl programs are scripts, so they start with the top-level code that isn't in any function definitions. – Barmar May 25 '13 at 19:10
  • Does the `perl -d` require a graphical environment? – Cratylus May 25 '13 at 19:16
  • So you recommend to run the script as a program under a debugger? Ok. But how can I also find out which other modules call routine of this module? – Cratylus May 25 '13 at 19:31
  • You have to know the main script that is being run in the application. You run that script with the `-d` option, set a breakpoint in the module you care about. Then wait for the breakpoint to be hit, do a stack trace, and you'll see the function in some other module that called it. – Barmar May 25 '13 at 19:33
  • Well. That is my main problem I don't know the main script.It is a large script repository – Cratylus May 25 '13 at 19:56
  • +1 for the FAQ link and [B::Xref](http://perldoc.perl.org/B/Xref.html). I wouldn't recommend debugger as *first* option though. – Dallaylaen May 25 '13 at 20:17
4

Most of the time getting a stack trace (along with some debugging info) is a good start. One can use standard Carp module to generate stack traces:

use Carp;  
print_to_log(Carp::longmess("We're here"));

Or there's an object-oriented module for that as well.

Dallaylaen
  • 5,268
  • 20
  • 34
4

To get a dump of the call stack without modifying any code, you can use the perl command line to run your program under Carp::Always:

perl -MCarp::Always my_program.pl
Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • I get `Can't locate Carp/Always.pm in @INC ` – Cratylus May 26 '13 at 21:08
  • 1
    @Cratylus you might need to install the module from CPAN then, see `man cpan` (I guess it comes with Perl) or [cpanminus](http://search.cpan.org/perldoc?App%3A%3Acpanminus‎). See also [this discussion](http://stackoverflow.com/questions/5861292/which-cpan-installer-is-the-right-one-cpan-pm-cpanplus-cpanminus) – Dallaylaen May 27 '13 at 07:51