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.


In reply to How not to check if a module is installed by xdg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.