Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a system script in perl. I need some equivalent of bash -x to determine what is going wrong with the script. Is there something equivalent?

EDIT: What bash -x does is that it prints each line as it is evaluated. This makes debugging code that is just missing some path variable or file very easy.

like image 903
Šimon Tóth Avatar asked Oct 04 '10 01:10

Šimon Tóth


2 Answers

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...
like image 96
Chas. Owens Avatar answered Nov 03 '22 07:11

Chas. Owens


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);
like image 9
mob Avatar answered Nov 03 '22 05:11

mob