What are some good Perl debugging methods?

前端 未结 18 1003
南笙
南笙 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:36

    Personally, I'm a big fan of Smart::Comments. It makes tracing dead simple, and there isn't any need to strip it out again, either.

    use Smart::Comments -ENV;
    ...
    sub myroutine {
        my ($self, @args) = @_ ;
        ### args: @args
        ...
    }
    

    If Smart_Comments has been set in the environment, the lines commencing with ### are converted to debug output, with Dumper() used automagically. If the environment variable isn't set, the debug stuff is completely inert.

    It has heaps of features and will produce progress bars, warnings, abort conditions as well as plain old debug output.

    Appropriate tests are all good, and I'm not dismissing a good test-driven development (TDD) development methodology, but when trying to get to the bottom of an existing bug, Smart::Comments is the go.

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

    If you don't like perl -d then Devel::REPL and Carp::REPL are both nice alternatives.

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

    Use Devel::SimpleTrace for the most elegant seamless stateless-debugging.

    perl -MDevel::SimpleTrace -we'warn "main"; sub foo{ warn "outer"; sub { warn "inner" } }; foo()->()'
    
    0 讨论(0)
  • 2020-12-12 21:39

    Some people use print statements in order to see what's going on in sections of a program that aren't doing what they thought the code would do. (I.e., as a way of checking what is actually contained in a variable at a given point of execution.)

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

    Writing tests can mostly decrease debugging time, I think.

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

    Depending on what you're doing, Log::Log4perl provides an easy way to manage the 'print' style of debugging particularly in bigger applications:

    • provides various logging levels (Debug, Info, Error, Warning, and Fatal)
    • controlled from configuration files (easy to have debugging on development box, only errors on production box, for example)
    • configurable by sections of your application (e.g., web application in one log file at one level, cron scripts in another at a different log level)
    • configurable by class - easy to quieten noisy modules, or add detailed debugging to somewhere deep within an application
    0 讨论(0)
提交回复
热议问题