Is there a way to turn on tracing in perl (equivalent to bash -x)?

百般思念 提交于 2019-11-30 07:52:00

Take a look at Devel::Trace or Devel::ebug.

Given this program named w.pl:

#!/usr/bin/perl

use strict;
use warnings;

my $answer = 42;

if ($answer == 6 * 9) {
    print "everything is running fine.\n";
} else {
    warn "there must be a bug somewhere...\n";
}

You can use Devel::Trace to watch the execution:

perl -d:Trace w.pl

Which produces the following output:

>> w.pl:6: my $answer = 42;
>> w.pl:8: if ($answer == 6 * 9) {
>> w.pl:11:     warn "there must be a bug somewhere...\n";
there must be a bug somewhere...

You should look at "perl -d" (turn on debugger) or "perl -c" (check your script before executing

The Devel::DumpTrace module has been available since 2011.

Sample usage:

$ cat demo.pl
# demo.pl
# a demonstration of Devel::DumpTrace
$a = 1;
$b = 3;
$c = 2 * $a + 7 * $b;
@d = ($a, $b, $c + $b);

$ perl -d:DumpTrace demo.pl
>>>>> demo.pl:3:        $a:1 = 1;
>>>>> demo.pl:4:        $b:3 = 3;
>>>>> demo.pl:5:        $c:23 = 2 * $a:1 + 7 * $b:3;
>>>>> demo.pl:6:        @d:(1,3,26) = ($a:1, $b:3, $c:23 + $b:3);
Ruel

Always include these statements in your perl scripts:

use strict;
use warnings;

If you want to debug it, use the -d switch. And here are the commands: http://www.domainavenue.com/pl-debug.htm

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!