in reply to How not to check if a module is installed

The way it's done now allows for the mutual inclusion of two modules. The way you suggest would cause an infinite loop.

The way it's done now takes into consideration that the re-running a module that failed in an uncontrolled fashion is a bad idea.

If the module dies in a controled fashion (by returning a specific value or by throwing a specific exception indicating it's safe to reload the module), you can circumvent the %INC check by using do or by clearing the appropriate entry in %INC.

(my $module = "$class.pm") =~ s{::}{/}g; if (!eval { require $module }) { die $@ if ((my $e = $@) !~ /.../); # Fix something ... # Try again delete $INC{$module}; require $module; }

Replies are listed 'Best First'.
Re^2: How not to check if a module is installed
by xdg (Monsignor) on Oct 24, 2007 at 16:42 UTC
    The way it's done now allows for the mutual inclusion of two modules. The way you suggest would cause an infinite loop.

    I didn't understand what you meant by this at first -- but after considering it, I think you're speaking of why %INC is set first. I agree. I wasn't suggesting changing it -- I just hadn't fully understood the consequences of what a failing "eval require" really does.

    More broadly, I think it shows a limitation of eval as an exception mechanism for module loading. If Foo or Bar die during require, they could easily leave the symbol table in an unstable state.

    As for your example, I'm actually not concerned with circumventing the %INC check. I just need to know if a module is installed (and working) -- and since I might ask that in more than one place in the code, some sort of cache of the first answer I get doing a require was what I needed.

    -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.