当我使用perl -d myscript.pl
调用我的Perl调试器时,调试器启动,但在我按n
(下一个)或c
(继续)之前,它不会执行任何代码。
无论如何调用调试器并默认运行代码直到遇到断点?
如果是这样,是否有任何语句可以在我的代码中用作断点,让调试器在命中时停止?
以下是我在.perldb
文件中的内容:
print "Reading ~/.perldb options.\n";
push @DB::typeahead, "c";
parse_options("NonStop=1");
这是我的hello_world.pl
文件:
use strict;
use warnings;
print "Hello world.\n";
$DB::single=1;
print "How are you?";
以下是运行的调试会话:perl -d hello_world.pl
:
Reading ~/.perldb options.
Hello world
main::(hello_world.pl:6): print "How are you?";
auto(-1) DB<1> c
Debugged program terminated. Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
DB<1> v
9563 9564
9565 sub at_exit {
9566==> "Debugged program terminated. Use `q' to quit or `R' to restart.";
9567 }
9568
9569 package DB; # Do not trace this 1; below!
DB<1>
换句话说,我的调试器会跳过print "How are you?"
,而是在程序完成后停止,这不是我想要的。
我想要的是调试器运行我的代码而不停在任何地方(也不是在开头,也不是在我的脚本末尾),除非我明确地拥有$DB::single=1;
语句,在这种情况下我希望它在运行下一行之前停止。有什么办法吗?
供参考,我正在使用:
$perl --version
This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux
答案 0 :(得分:12)
把
$DB::single = 1;
在任何在代码中设置永久断点的语句之前。 这也适用于编译时代码,并且可能是在编译阶段设置断点的唯一好方法。
要让调试器自动启动代码,您可以在代码中的@DB::typeahead
文件或编译时(.perldb
)块中操作BEGIN
数组。例如:
# .perldb file
push @DB::typeahead, "c";
或
BEGIN { push @DB::typeahead, "p 'Hello!'", "c" }
...
$DB::single = 1;
$x = want_to_stop_here();
您可以在"NonStop"
或.perldb
环境变量中设置PERLDB_OPTS
选项:
PERLDB_OPTS=NonStop perl -d myprogram.pl
所有这些(以及更多内容)都会在perldebug
和perl5db.pl
解决最近更新中提出的问题。在./perldb
中使用以下内容:
print "Reading ~/.perldb options.\n";
push @DB::typeahead, "c";
parse_options("inhibit_exit=0");
答案 1 :(得分:2)
同时查看Enbugger。关于调试器的主题,请参阅Devel::Trepan,它也适用于Enbugger。