pryrt has asked for the wisdom of the Perl Monks concerning the following question:

Given the SSCCE below, Devel::Cover v1.40 shows missing "statement" coverage for a line of code that is being run:
#!perl use 5.012; # strict, // use warnings; { my $i = 1; my $j = 2; sub get { $i + $j; } } get();
and running with
perl -MDevel::Cover x.pl cover

why does it say only 90.9% coverage?

----- ------ ------ ------ ------ ------ ------ ------ File stmt bran cond sub pod time total ----- ------ ------ ------ ------ ------ ------ ------ x.pl 90.9 n/a n/a 100.0 n/a 100.0 92.8 Total 90.9 n/a n/a 100.0 n/a 100.0 92.8 ----- ------ ------ ------ ------ ------ ------ ------

The coverage report shows the following table:
linestmtbrancondsubpodtimecode
1#!perl
2
1
1
0
0
use 5.012; # strict, //
3
1
1
1
0
0
0
use warnings;
4
5{
6
1
0
0
0
    my $i = 1;
7
1
0
    my $j = 2;
8    sub get {
9
1
        $i + $j;
10    }
11}
12
1
1
74157
0
get();

... but I cannot see why line 6 has two rows in the table, or why it says that the second row for that line was never run; there's only one statement, as far as I can tell. Removing the  = 1 or changing the order of the declarations doesn't change the fact that the first row in that block claims to never run.

If I change to do { ... }; instead of just a block, then line 6 cleans up and only shows one row in the table, but now the line 4 do { shows it was run 0 times:

linestmtbrancondsubpodtimecode
1#!perl
2
1
1
5875
0
use 5.012; # strict, //
3
1
1
1
0
0
0
use warnings;
4
5
0
0
do {
6
1
0
    my $i = 1;
7
1
0
    my $j = 2;
8    sub get {
9
1
        $i + $j;
10    }
11
1
58427
};
12
1
0
get();

If I convert the block to a sub init { ... } and then call it with init(), then the sub init { line shows as uncovered statement, just like the do { version.

I can work around it by getting rid of the block, so eliminating the coverage-flag isn't a problem (but I want the block in my real code to encapsulate the my variables to be private just for my get function, so I'm not going to do that). Or I can cheat with # uncoverable statement count:2. But my curiosity has been piqued as to why I should need a workaround at all.

Replies are listed 'Best First'.
Re: mystery: Devel::Cover showing missing statement coverage on a line that is being run
by haukex (Archbishop) on May 13, 2023 at 07:29 UTC

    I can confirm the same results, and it looks like a bug to me. The minimal test case I was able to reduce it to is { sub x {} } x() (a print can be inserted into the sub to prove it actually does run). The issue trackers contain a few bugs for similar kinds of issues where coverage is not reported correctly, so you may want to open a new bug report.

      Thanks for the confirmation. Created issue 324.