0

How can I run a Perl script in debugging mode (like "bash -x" for shell scripts)?

I tried the -w parameter like the following example:

#!/usr/bin/perl -w

But it didn't work out.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    What do you mean by "it didn't work out"? Please show how you tried to run your script and what the error was. – dg99 Jan 08 '15 at 18:48
  • 4
    Welcome to Stack Overflow. Please read the [About] page soon. The `-w` will have worked, but it doesn't run the debug (it adds warnings while Perl is compiling the script). See [perldebug](http://perldoc.perl.org/perldebug.html) for how to run and use the Perl debugger. Note that a question that says "it didn't work" without showing the code, the actual result and the expected result is not really answerable -- such questions are often closed as 'off topic'. – Jonathan Leffler Jan 08 '15 at 18:49
  • 3
    "-w" activates warnings in perl. See [here](http://perldoc.perl.org/perldebug.html) for debugging. – Jens Jan 08 '15 at 18:51
  • 1
    Possible duplicate of [Is there a way to turn on tracing in perl (equivalent to bash -x)?](https://stackoverflow.com/questions/3852395/is-there-a-way-to-turn-on-tracing-in-perl-equivalent-to-bash-x) – delirium Jul 06 '18 at 12:41

3 Answers3

6
use strict;
use warnings;
use diagnostics;

are enough IMO.

If you want to use debugger then check out: perldebug

You can use built in command line debugger as:

perl -d yourcode.pl

Also see:

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
5

perl -Dt is kind of like bash -x , but you need to specifically compile perl to allow that kind of tracing.

But with any perl, you can run a script using the debugger:

perl -d yourscriptname yourscriptargs

See perldebtut for starters.

Baba
  • 852
  • 1
  • 17
  • 31
ysth
  • 96,171
  • 6
  • 121
  • 214
4

For bash -x style trace of a Perl script, check out Devel::DumpTrace. Example:

demo.pl:

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

program output:

      $ 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);
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
mob
  • 117,087
  • 18
  • 149
  • 283