Azhrarn has asked for the wisdom of the Perl Monks concerning the following question:

Currently I'm working on a project using POE which may possibly use other event loops (Tk, Gtk, etc). Due to the way POE works, any other event loops that it supports need to be loaded prior to POE itself. And due to the way my program works (gui type defined by a config file, and some cross platform issues), I don't know which one I will be using until runtime. Also, it is likely that the modules that aren't being used won't even be installed, although I don't think that should make any difference if this is done right.


I came across this node, which had a very similiar problem, but I'm not sure exactly how that will work when I need to call a dependant module (POE) after the requires (as they aren't BEGIN blocks.) Here's what I have currently:

if( lc($config->outputs('gui')) eq 'tk' ) { require Tk; import Tk; } elsif( lc($config->outputs('gui')) eq 'gtk' ) { require Gtk; import Gtk; } require POE; import POE;


Note that I have more code prior to this that loads the $config object and so on, and after the POE module is loaded, I have to instantiate the Components I will be using. I won't actually be using Tk or whatever until inside the called Component, I'll just be posting an event to tell it what kind of GUI it will need to use. I'll just be initializing the main object and starting POE. I haven't done all that code yet, as it depends on what I do here a bit. But if you need more detail let me know. Also, I'm not sure if I need all the imports here, will it be better to just to that inside the Component itself?

Replies are listed 'Best First'.
Re: Selective module loading
by dragonchild (Archbishop) on Jul 15, 2003 at 16:50 UTC
    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.

      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 :)