当我运行一个Perl脚本时,如何进行调试?例如,在ksh中,我会添加
-x
标志。但是在Perl中怎么做?perl -d your_script.pl args
这是如何调试Perl的方法。它会将您进入一个交互式的类似gdb
的命令行调试器。
要在Perl调试器下运行您的脚本,您应该使用-d
选项:
perl -d script.pl
但是Perl很灵活。它提供了一些钩子,你可以强制调试器按照你的意愿工作。
因此,要使用不同的调试器,您可以执行以下操作:
perl -d:DebugHooks::Terminal script.pl
# OR
perl -d:Trepan script.pl
有几个非常有趣的Perl模块钩入了Perl调试器内部: Devel::NYTProf 和 Devel::Cover
还有许多其他模块。
use v5.14;
say 1;
say 2;
say 3;
在终端1上启动一个监听器,监听任何主机和端口(这里是localhost:12345
):
$ nc -v -l localhost -p 12345
如果需要readline支持,请使用rlwrap(你也可以在perl -d
中使用):
$ rlwrap nc -v -l localhost -p 12345
并在另一个终端(比如终端2)上开始测试:
$ PERLDB_OPTS="RemotePort=localhost:12345" perl -d test
终端1上的输入/输出:
Connection from 127.0.0.1:42994
Loading DB routines from perl5db.pl version 1.49
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(test:2): say 1;
DB<1> n
main::(test:3): say 2;
DB<1> select $DB::OUT
DB<2> n
2
main::(test:4): say 3;
DB<2> n
3
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<2>
第二个终端的输出:
1
select $DB::OUT
-d
选项的文档链接。 - DVKuse strict; use warnings;
。如果这不起作用,请进行步骤2:使用perl -d
命令。 - Graeme Perrow