简体   繁体   English

如何调试 Perl 脚本?

[英]How can I debug a Perl script?

When I run a Perl script, how can I debug it?当我运行 Perl 脚本时,如何调试它? For example, in ksh I add the -x flag.例如,在 ksh 中我添加了-x标志。 But how I do the same in Perl?但是我如何在 Perl 中做同样的事情呢?

perl -d your_script.pl args

is how you debug Perl.这就是调试 Perl 的方式。 It launches you into an interactive gdb -style command line debugger.它使您进入交互式gdb风格的命令行调试器。

To run your script under the Perl debugger you should use the -d switch:要在 Perl 调试器下运行您的脚本,您应该使用-d开关:

perl -d script.pl

But Perl is flexible.但是 Perl 是灵活的。 It supplies some hooks, and you may force the debugger to work as you want它提供了一些钩子,你可以强制调试器按照你的意愿工作

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 and Devel::Cover有几个最有趣的 Perl 模块挂接到 Perl 调试器内部: Devel::NYTProfDevel::Cover

And many others .还有很多其他的。

If using an interactive debugger is OK for you, you can try perldebug .如果您可以使用交互式调试器,您可以尝试perldebug

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 的-x之类的问题,请查看Devel::Trace模块,它执行类似的操作。

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.EclipseEPIC一起使用:它为您提供了一个具有调试可能性的不错的 IDE,包括放置断点的能力和用于检查变量值的Perl Expression View

If you want to do remote debugging (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 提供基本支持。

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增强打印语句)

Note that the Perldebugger can also be invoked from the scripts shebang line, which is how I mostly use the -x flag you refer to, to debug shell scripts.请注意, Perldebugger也可以从 scripts shebang 行调用,这就是我主要使用您引用的-x标志来调试 shell 脚本的方式。

#! /usr/bin/perl -d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM