What are some good Perl debugging methods?

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

    During development, I like to embed printf statements in strategic places (not too many) which are enabled with a debug flag like this:

    printf("h='$h', j='$j', ... (%d)\n", __LINE__) if $debug;
    

    where the debug flag is defined at the top of the script:

    my $debug = $ENV{DEBUG} || 0;
    

    Now instead of having to remember to comment out all of the printf lines, I just run the script as follows:

    DEBUG=1 ./script.pl
    

    After testing when everything is ready for production, the debug lines can be removed:

    cat script.pl | grep -v 'if $debug;'
    

提交回复
热议问题