in reply to Re^2: Failed require looks like it succeeded
in thread Failed require looks like it succeeded

You could always delete the entry in INC so that the second require works as expected. Another solution would be to have modules register themselves and only try to load unregistered module. I've used both solutions in systems where modules might be created/fixed while the main app is running but I don't want to restart the main app.


___________
Eric Hodges
  • Comment on Re^3: Failed require looks like it succeeded

Replies are listed 'Best First'.
Re^4: Failed require looks like it succeeded
by Anonymous Monk on Mar 21, 2005 at 22:34 UTC
    Both of those approaches could be used to work around the problem in the examples I gave so far, but actually the situation is slightly more complicated:

    # foo.pl for (1..2) { eval "use bar"; print "attempt $_: ", ($@ || 'success'), "\n"; } # bar.pm eval "require baz"; if ($@) { die "bar will be unavailable because baz didn't load properly."; } 1; # baz.pm use missingdependency;

    So adding delete $INC{bar.pm} if $@; to foo.pl isn't sufficient--I also need to delete baz.pm. This would be easy if I could modify bar.pm, but I don't want to--bar.pm is a third-party plugin that was written for earlier versions of foo.pl, and I'd prefer not to break backwards compatibility for the plugins.

    But the following works, without any changes to bar.pm or baz.pm:

    # foo.pl for (1..2) { my %saveinc = %INC; eval "use bar"; if ($@) { for my $package(keys %INC) { delete $INC{$package} if ! exists $saveinc{$package}; } } print "attempt $_: ", ($@ || 'success'), "\n"; }

    I think that'll basically do the trick. Maybe I'll also delete the symbol table of the packages I delete from %INC, in case some of them actually loaded successfully, to avoid getting warnings about redefinitions later on.

    Thanks everyone for all the ideas.