This was an amusing annoying little bug I recently discovered.
Imagine that module Foo in Foo.pm depends on module Bar:
package Foo; use Bar; 1;
But module Bar in Bar.pm is broken or missing or otherwise fails:
package Bar; 0;
Now, imagine that you have some code that checks if Foo is available using eval "require Foo" and, further, you wind up making that check more than once:
use Test::More tests => 2; for (1 .. 2) { eval "require Foo"; ok( $@, "Saw error loading Foo" ); }
And here's the result:
1..2 ok 1 - Saw error loading Foo not ok 2 - Saw error loading Foo # Failed test 'Saw error loading Foo' # at check_foo.pl line 5. # Looks like you failed 1 test of 2.
It turns out that $INC{Foo.pm} is set during require before Foo.pm is actually loaded (i.e. regardless of whether it succeeds or fails). The first eval prevents the code from dying, but $INC{Foo.pm} is still set so the second time around, require just shortcuts and returns 1.
So if eval "require Foo" fails because of something that Foo requires, you only get one chance to see if Foo is unavailable.
That suggests to me that it's wise to always use a subroutine that caches the first result just in case you ever need to check more than once:
my %have_module; sub have_module { my $mod = shift; if ( ! exists $have_module{$mod} ) { $have_module{$mod} = eval "require $mod"; } return $have_module{$mod}; }
Anyone know if some CPAN modules can help get this right?
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How not to check if a module is installed (bug)
by tye (Sage) on Oct 24, 2007 at 15:36 UTC | |
by xdg (Monsignor) on Oct 24, 2007 at 16:46 UTC | |
|
Re: How not to check if a module is installed
by ikegami (Patriarch) on Oct 24, 2007 at 15:21 UTC | |
by xdg (Monsignor) on Oct 24, 2007 at 16:42 UTC | |
|
Re: How not to check if a module is installed
by shmem (Chancellor) on Oct 24, 2007 at 15:10 UTC | |
by xdg (Monsignor) on Oct 24, 2007 at 16:06 UTC | |
|
Re: How not to check if a module is installed
by goibhniu (Hermit) on Oct 25, 2007 at 19:59 UTC |