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