I have encountered an anomaly in my use of Devel::Cover.

Suppose that I have two packages, Alpha and Beta, each of which
implements a sayhowdy() method, albeit slightly differently from each other.

  # in Alpha.pm

  sub sayhowdy {
    my $self = shift;
    my $string = q{};
    open my $fh, ">", \$string;
    print $fh "Howdy, alpha\n";
    close $fh;
    return $string;
  }

  # in Beta.pm

  sub sayhowdy {
    my $self = shift;
    my $string = q{};
    open my $fh, ">", \$string;
    print $fh "Howdy, beta\n";
    close $fh;
    return $string;
  }

(You can play along at home by downloading this tarball:
http://thenceforward.net/perl/misc/Alpha-0.01.tar.gz.)

The only other non-trivial difference between the two packages is that
the final statement in Beta.pm assigns Beta::sayhowdy() to
Alpha::sayhowdy() should Alpha::sayhowdy() somehow be (or get) undefined.

  *Alpha::sayhowdy = \&sayhowdy if ! defined &Alpha::sayhowdy;

I can write the following test file to demonstrate that this
reassignment can actually be made:

  use Test::More qw( no_plan);

  use_ok( q{Alpha} );

  my $class = q{Alpha};
  my $self = $class->new();

  isa_ok($self, $class);
  can_ok($class, q{sayhowdy} );

  is($self->sayhowdy(), qq{Howdy, alpha\n},
      "alpha sayhowdy okay");

  undef &Alpha::sayhowdy;
  eval { $self->sayhowdy(); };
  like($@, qr/^Undefined subroutine &Alpha::sayhowdy called/,
      "Alpha::sayhowdy() is now undefined");


  require_ok('Beta');
  is($self->sayhowdy(), qq{Howdy, beta\n},
      "Beta::sayhowdy is now Alpha::sayhowdy");

... which produces:

  [Alpha] 539 $ prove -vb t/03_gamma.t
  t/03_gamma....ok 1 - use Alpha;
  ok 2 - The object isa Alpha
  ok 3 - Alpha->can('sayhowdy')
  ok 4 - alpha sayhowdy okay
  ok 5 - Alpha::sayhowdy() is now undefined
  ok 6 - require Beta;
  ok 7 - Beta::sayhowdy is now Alpha::sayhowdy
  1..7
  ok
  All tests successful.
  Files=1, Tests=7,  0 wallclock secs

So far, so good.  But when I run Devel::Cover on this distribution, the
coverage report treats the glob assignment line above as if it weren't
even there.

  $ cover -delete
  $ make test HARNESS_PERL_SWITCHES=1
  $ cover cover_db --report=txt > coverage.txt

  # in the section of coverage.txt reporting on lib/Beta.pm:
  # (for simplicity, displaying only statement coverage)

  blib/lib/Beta.pm

  line  err   stmt   code
  1                  package Beta;
  2                  #$Id#
  3              2   use strict;

  # [snip]

  16             2       print $fh "Howdy, beta\n";
  17             2       close $fh;
  18             2       return $string;
  19                 }
  20
  21                 *Alpha::sayhowdy = \&sayhowdy
                         if ! defined &Alpha::sayhowdy;
  22
  23                 1;

The test file above clearly exercises line 21, but Devel::Cover reports
nothing at all about that line.  Has anyone encountered/can anyone
explain this?

Thanks in advance.

Jim Keenan

Reply via email to