everbox
everbox

Reputation: 1199

How to set breakpoint on a particular file in Perl program?

My Perl program looks like:

foo/
foo/bar/
for/bar/test.pm
foo.pm
foo/bar.pm
test.pl

and use perl test.pl to start the program. I want to debug a sub in foo/bar/test.pm. How to set a breakpoint on that sub?

Upvotes: 27

Views: 22868

Answers (3)

Teepeemm
Teepeemm

Reputation: 4518

You can enter f Module.pm to tell the debugger to look at that particular module. Once you've done that, b line_number will stop at that line in Module.pm.

Once the original script has passed use Module, then b subroutine will stop at that subroutine. The only catch here is that you can't make your first two debugger commands f Module.pm; b subroutine because when the script begins, it hasn't passed use Module, so Module.pm hasn't loaded yet, which means that perl can't see that Module.pm has subroutine.

Upvotes: 1

ikegami
ikegami

Reputation: 386706

Just use the fully qualified name of the sub as the argument to b:

b foo::bar::test::subname

Example:

$ perl -d -e'use CGI; CGI->new'
...
main::(-e:1):   use CGI; CGI->new
  DB<1> b CGI::new
  DB<2> r
CGI::new(.../CGI.pm:337):
337:      my($class,@initializer) = @_;
  DB<2> q

Upvotes: 18

socket puppet
socket puppet

Reputation: 3219

To debug a perl script, use the -d switch to invoke the debugger.

perl -d test.pl

Within the debugger you can use b <line no> to set a breakpoint in the current file. Sometimes it is a hassle to set a breakpoint in a file that hasn't been loaded yet or that was loaded a long time ago, so you can also put the line

$DB::single = 1;

anywhere in any perl program, and the debugger will break immediately after it executes that line. This is also a good way (the only way?) to set a breakpoint in code that will be run at compile time.

Upvotes: 41

Related Questions