如何调试Perl脚本?

时间:2021-04-22 07:15:50

When I run a Perl script, how can I debug it? For example, in ksh I add the -x flag. But how I do the same in Perl?

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

7 个解决方案

#1


10  

perl -d your_script.pl args

is how you debug Perl

是你如何调试Perl

#2


8  

If using an interactive debugger is OK for you, you can try perldebug

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

#3


6  

To run your script under perl debugger you should use -d switch:

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

perl -d script.pl

But perl is flexible. It supply some hooks and you may force debugger to work as you want

但perl很灵活。它提供了一些钩子,你可以强制调试器按你的意愿工作

So to use different debuggers you may do:

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

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

Look these modules here and here

在这里和这里查看这些模块

There are several most interesting perl modules that hook into perl debugger internals: Devel::NYTProf, Devel::Cover

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

And many others

还有很多其他人

#4


4  

I would also recommend using the Perl debugger.

我还建议使用Perl调试器。

However, since you asked about something like shell's -x have a look at the Devel::Trace module which does something similar.

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

#5


4  

Use Eclipse with EPIC: It gives you a nice IDE with debugging possibilities, including the ability to place breakpoints and the Perl Expression View for inspecting the value of variables.

将Eclipse与EPIC结合使用:它为您提供了一个具有调试可能性的IDE,包括放置断点的能力和用于检查变量值的Perl表达式视图。

#6


3  

The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.

最有效的调试工具仍然是仔细考虑,加上明智的打印语句。

Brian Kernighan, "Unix for Beginners" (1979)

Brian Kernighan,“Unix初学者”(1979)

(And enhancing print statements with Data::Dumper)

(并使用Data :: Dumper增强打印语句)

#7


0  

If you want to do remote debug (for cgi or if you don't want to mess output with debug command line) use this:

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

given test:

给定测试:

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

Start a listener on whatever host and port on terminal 1 (here localhost:12345):

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

$ nc -v -l localhost -p 12345

for readline support use rlwrap (you can use on perl -d too):

对于readline支持使用rlwrap(你也可以在perl -d上使用):

$ rlwrap nc -v -l localhost -p 12345

And start the test on another terminal (say terminal 2):

并在另一个终端(比如终端2)上开始测试:

$ PERLDB_OPTS="RemotePort=localhost:12345" perl -d test

Input/Output on terminal 1:

终端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> 

Output on terminal 2:

终端2的输出:

1

Note the sentence if you want output on debug terminal

如果要在调试终端上输出,请注意该句子

select $DB::OUT

If you are vim user, install this plugin: dbg.vim which provides basic support for perl

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

#1


10  

perl -d your_script.pl args

is how you debug Perl

是你如何调试Perl

#2


8  

If using an interactive debugger is OK for you, you can try perldebug

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

#3


6  

To run your script under perl debugger you should use -d switch:

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

perl -d script.pl

But perl is flexible. It supply some hooks and you may force debugger to work as you want

但perl很灵活。它提供了一些钩子,你可以强制调试器按你的意愿工作

So to use different debuggers you may do:

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

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

Look these modules here and here

在这里和这里查看这些模块

There are several most interesting perl modules that hook into perl debugger internals: Devel::NYTProf, Devel::Cover

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

And many others

还有很多其他人

#4


4  

I would also recommend using the Perl debugger.

我还建议使用Perl调试器。

However, since you asked about something like shell's -x have a look at the Devel::Trace module which does something similar.

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

#5


4  

Use Eclipse with EPIC: It gives you a nice IDE with debugging possibilities, including the ability to place breakpoints and the Perl Expression View for inspecting the value of variables.

将Eclipse与EPIC结合使用:它为您提供了一个具有调试可能性的IDE,包括放置断点的能力和用于检查变量值的Perl表达式视图。

#6


3  

The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.

最有效的调试工具仍然是仔细考虑,加上明智的打印语句。

Brian Kernighan, "Unix for Beginners" (1979)

Brian Kernighan,“Unix初学者”(1979)

(And enhancing print statements with Data::Dumper)

(并使用Data :: Dumper增强打印语句)

#7


0  

If you want to do remote debug (for cgi or if you don't want to mess output with debug command line) use this:

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

given test:

给定测试:

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

Start a listener on whatever host and port on terminal 1 (here localhost:12345):

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

$ nc -v -l localhost -p 12345

for readline support use rlwrap (you can use on perl -d too):

对于readline支持使用rlwrap(你也可以在perl -d上使用):

$ rlwrap nc -v -l localhost -p 12345

And start the test on another terminal (say terminal 2):

并在另一个终端(比如终端2)上开始测试:

$ PERLDB_OPTS="RemotePort=localhost:12345" perl -d test

Input/Output on terminal 1:

终端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> 

Output on terminal 2:

终端2的输出:

1

Note the sentence if you want output on debug terminal

如果要在调试终端上输出,请注意该句子

select $DB::OUT

If you are vim user, install this plugin: dbg.vim which provides basic support for perl

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