in reply to Selective module loading

A thought - SAX does something similar to this. I haven't looked at the code, but I suspect it might look something like:
my @modules = qw(Tk Gtk); my $loaded = 0; foreach my $module (@modules) { eval { require $module; import $module; }; next if $@; $loaded = 1; last; } die "Can't load anything (@modules)\n" unless $loaded; require POE; import POE;

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Selective module loading
by Azhrarn (Friar) on Jul 15, 2003 at 17:43 UTC
    Is this the part you are referring to? (From XML::SAX)
    sub add_parser { my $class = shift; my ($parser_module) = @_; if (!$known_parsers) { $class->load_parsers(); } # first load module, then query features, then push onto known_par +sers, my $parser_file = $parser_module; $parser_file =~ s/::/\//g; $parser_file .= ".pm"; require $parser_file; my @features = $parser_module->supported_features(); my $new = { Name => $parser_module }; foreach my $feature (@features) { $new->{Features}{$feature} = 1; } # If exists in list already, move to end. my $done = 0; my $pos = undef; for (my $i = 0; $i < @$known_parsers; $i++) { my $p = $known_parsers->[$i]; if ($p->{Name} eq $parser_module) { $pos = $i; } } if (defined $pos) { splice(@$known_parsers, $pos, 1); push @$known_parsers, $new; $done++; } # Otherwise (not in list), add at end of list. if (!$done) { push @$known_parsers, $new; } return $class; }

    Seems similiar, but I'm still not sure how that affects methods and such called after the require. Although after looking at perlmod a bit more, I think I may be alright with the existing code. As the problems seem to stem when you depend on exported (well... imported really) things due to the BEGIN blocks that normally come into play, which I can get around by specifying the namespace. ...I think :)