What are some good Perl debugging methods?

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

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

18条回答
  •  旧时难觅i
    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.

提交回复
热议问题