What are some good Perl debugging methods?

前端 未结 18 1004
南笙
南笙 2020-12-12 21:12

Are there other ways for debugging Perl programs apart from Data::Dumper and perl -d?

相关标签:
18条回答
  • 2020-12-12 21:44

    Generally I use

    perl -d
    

    for debugging.

    You can also use the Eclipse Perl Integration (EPIC) plug-in for Eclipse. It offers a rich debugging environment available and integrated with the EPIC Perl development environment. You can use it, and it is generally helpful.

    0 讨论(0)
  • 2020-12-12 21:46

    Test::More for writing basic tests, Hook::LexWrap, Test::MockObject, Test::Deep, Test::MockTime, Test::WWW::Mechanize and many others for advanced tests.

    Attribute::Signature for checking sub parameters. Carp::Assert for contract-based programming.

    Devel::Ebug::Wx or Devel::ptkdb (and soon better support in Padre) can be used for easier debugging.

    0 讨论(0)
  • 2020-12-12 21:46

    Debug::Statements provides an easy way to insert and enable/disable print statements for debugging.

    The d() function prints the name of your variable, its value, and your subroutine name. The implementation been optimized to minimize programmer keystrokes.

    Here is sample code to get you started:

    my $myvar = 'some value';
    my @list = ('zero', 1, 'two', "3");
    my %hash = ('one' => 2, 'three' => 4);
    
    use Debug::Statements;
    my $d = 1;
    d "Hello, World!";
    d '$myvar';
    d '@list %hash';
    

    Output:

    DEBUG sub mysub:  Hello, World!
    DEBUG sub mysub:  $myvar = 'some value'
    DEBUG sub mysub:  @list = [
      'zero',
      1,
      'two',
      '3'
    ]
    DEBUG sub mysub:  %hash = {
      'one' => 2,
      'three' => 4
    }
    

    Many options are available to customize the output. Full documentation can be found on CPAN.

    0 讨论(0)
  • 2020-12-12 21:48

    Emacs, hands down:

    emacs my_script.pl
    M-x perldb
    

    Emacs will prompt you:

    Run perldb (like this): perl my_script.pl
    Hit enter (or add command line switches)
    

    Now use the debugger as usual.

    Type 'c' to continue executing the code, which will now follow your code as you execute through it.

    Emacs is fully integrated with its debuggers and will make debugging Perl code nearly trivial.

    0 讨论(0)
  • 2020-12-12 21:50

    There are lots of things out there to help you:

    • Devel::Trace - print every line that executes
    • Carp::REPL - drop into a REPL* when the code throws a warning
    • Devel::ebug - a debugger you can control from Perl code
    • Enbugger - use a debugger at runtime regardless of whether your process was started with debugging
    0 讨论(0)
  • 2020-12-12 21:50

    Some Other methods

    CGI::Dump
    
    Benchmark
    
    Command-line options 
    
    __DATA__ & <DATA> 
    
    $.
    
    __FILE__ & __LINE__ 
    
    warn() & die() 
    
    0 讨论(0)
提交回复
热议问题