如何调试Perl脚本?

时间:2011-02-09 13:37:51

标签: perl debugging

当我运行Perl脚本时,我该如何调试它?例如,在ksh中我添加了-x标志。但我如何在Perl中做同样的事情呢?

8 个答案:

答案 0 :(得分:11)

perl -d your_script.pl args

是你如何调试Perl

答案 1 :(得分:8)

如果您使用交互式调试器,则可以尝试perldebug

答案 2 :(得分:7)

要在perl调试器下运行脚本,您应该使用-d开关:

perl -d script.pl

但perl非常灵活。它提供了一些钩子,您可以强制调试器按您的需要工作

因此,您可以使用不同的调试器:

perl -d:DebugHooks::Terminal script.pl
# OR
perl -d:Trepan script.pl

查看这些模块herehere

有几个最有趣的perl模块挂钩到perl调试器内部:Devel::NYTProfDevel::Cover

many others

答案 3 :(得分:4)

我还建议使用Perl debugger

但是,既然你问过像shell -x这样的东西看看Devel::Trace模块做了类似的事情。

答案 4 :(得分:4)

EclipseEPIC一起使用:它为您提供了一个很好的IDE,它具有调试功能,包括放置断点和Perl Expression View来检查变量值的能力。

答案 5 :(得分:2)

  

最有效的调试工具仍然是careful thought   明智的印刷陈述。

     

Brian Kernighan, "Unix for Beginners" (1979)

(并使用Data::Dumper

增强打印报表

答案 6 :(得分:0)

如果你想进行远程调试(对于cgi,或者如果你不想用调试命令行搞乱输出),请使用:

给出测试:

use v5.14;
say 1;
say 2;
say 3;

在终端1上的任何主机和端口上启动监听器(此处为localhost:12345):

$ nc -v -l localhost -p 12345

使用rlwrap获取readline支持(您也可以在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> 

终端2上的输出:

1

如果要在调试终端上输出

,请注意该句子
select $DB::OUT

如果您是vim用户,请安装此插件:dbg.vim,它为perl提供基本支持

答案 7 :(得分:0)

请注意,Perldebugger也可以从脚本shebang行中调用,这就是我通常使用您引用的-x标志来调试shell脚本的方法。

#! /usr/bin/perl -d