17

I want the perl program launch debugger when some condition hit. Some other language has debug() statement supported by library, is there any similar statement in perl?

miku
  • 181,842
  • 47
  • 306
  • 310
Thomson
  • 20,586
  • 28
  • 90
  • 134

4 Answers4

36

If I understand you correctly, you need to use a specific debugger variable in your code - $DB::single. Setting this to a true value in your code will cause the debugger to stop on that line.

$x = 1234;
$DB::single = 1;
enter_problematic_sub_now();

Your code will then stop at the line with $DB::single set to 1.

Of course, if you can't actually ensure that your code is running in the debugger then you have another problem entirely. You will need to run your code via perl -d as well.

Nic Gibson
  • 7,051
  • 4
  • 31
  • 40
  • Do you mean if my program doesn't run in debugger, I can't use $DB::single as expected? – Thomson Jan 14 '11 at 13:45
  • 3
    @Thompson: Yes, that's right. If you want a Perl program to be debugged, you must run it under a debugger. To the best of my knowledge, this kind of thing holds true for most sorts of source-based real-time debugging in other languages — postmortems of coredumps notwithstanding. – tchrist Jan 14 '11 at 15:11
  • @tchrist I'm not sure how it works internally in Python, but I can do `import pdb; pdb.set_trace()` anywhere in the code to break at that point and enter the debugger. I don't have to run the script with any special debug modifiers. – yati sagade Oct 20 '15 at 09:49
6

Have you tried adding the -d switch to the shebang line at the top of your script? Something like

#!/usr/bin/perl -d
use strict;
use warnings;
$|=1;$\="\n";

print "Test";

It really depends exactly how it gets launched, but at least in simple cases this should start the debugger.

Edit: You can then set a breakpoint on a specific line with a certain condition using

> b [line] [condition]

and hit

> c

to continue with running the script - the debugger will stop at the specified line when the condition is met

ivancho
  • 899
  • 6
  • 10
  • That is great. Is there similar syntax on windows? – Thomson Jan 15 '11 at 02:35
  • 1
    I believe perl will respect the switches even if the actual perl executable launching is not the one listed in the shebang - ie, even if the above script is started as, say, C:\Perl\bin\perl.exe script.pl, it should still start the debugger – ivancho Jan 15 '11 at 18:52
2

Well, there is something which will enable you to do something like breakpoints, but the functionality is wider: Perl Debugger.

daxim
  • 39,270
  • 4
  • 65
  • 132
Thariama
  • 50,002
  • 13
  • 138
  • 166
2

Essentially the -d switch allows you to communicate with the perl executable, and allows the perl executable to communicate with you.

More

daxim
  • 39,270
  • 4
  • 65
  • 132
miku
  • 181,842
  • 47
  • 306
  • 310
  • My piece of perl code is part of large system, and i can't control how it is launched. It seems i can only revise to source file to do so. – Thomson Jan 14 '11 at 13:28
  • 3
    @Thomson Tan: In that case, logging would work better. Take a look at [Log::Log4perl](http://search.cpan.org/perldoc?Log::Log4perl). – Michael Carman Jan 14 '11 at 16:07