tags:

views:

129

answers:

3

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.

+1  A: 

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.

Ruel
+13  A: 

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...
Chas. Owens
+2  A: 

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

Raghuram
Be warned, "before executing" doesn't mean no code runs. Any code in a `BEGIN` block (or in a module than has been included with `use`) will run even if you use the `-c` option: `perl -cle 'BEGIN { print "oops, this runs" } print "but this does not"'`.
Chas. Owens