I encountered a real-world case where Test::More's use_ok() passed when the specified package had a fatal syntax error. I'm looking for advice about whether I should file a bug, or fix my code that triggered the false positive. Read on for details...

Consider two packages Foo.pm and Bar.pm:

--- Foo.pm ---
package Foo;
my $has_Bar = eval {require Bar; 1;};
sub print_msg {
  $has_Bar ? Bar->print("Hello") : print "Hello";
}
1;
--- Bar.pm ---
package Bar;

sub print { # egregious syntax error
1;
--- t/use.t ---
use Test::More tests => 2;
use_ok('Foo');
use_ok('Bar');


Then if I run "perl t/use.t" I get the surprising result:
  1..2
  ok 1 - use Foo;
  ok 2 - use Bar;

The problem is that Foo trapped Bar's syntax error in the eval, so % INC{Bar} is defined and use_ok() sees it as a success.

Advice? While this example is contrived, the "eval { require ... }" idiom is used often in the wild, so this is not a wholly unrealistic scenario.

Chris
--
Chris Dolan, Software Developer, Clotho Advanced Media Inc.
608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703
vCard: http://www.chrisdolan.net/ChrisDolan.vcf

Clotho Advanced Media, Inc. - Creators of MediaLandscape Software (http://www.media-landscape.com/) and partners in the revolutionary Croquet project (http://www.opencroquet.org/)


Reply via email to