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

I have an application which I'd like to make more modular..Towards that end I have one base app which checks a config file for a list of modules to require at run time. The problem is that while I have a standard api for interfacing with each module I dont know how to reference them within the core program. The require runs in a loop replacing the path/module name in a var based on the config file contents. I dont know how to detect and reference or set a way to reference the module after the require (assuming it's even possible as I'm wanting to do it). Thanks in advance..

20040119 Edit by BazB: Changed title from 'unknown requires?'

Replies are listed 'Best First'.
Re: Referencing required modules?
by Old_Gray_Bear (Bishop) on Jan 19, 2004 at 16:17 UTC
    If I read you correctly, what you want is something like this:
    my $mod_ref; my %modules; while (<CONFIG>) { chomp; my $mod_name =$_; my $mod_ref = My::Base::Class -> new($mod); $modules{$mod_name} = $mod_ref; }

    Note: Untested, just off the top of the (only slightly caffeinated as yet) head.

    Use the new() method of your base class as a Factory Object to build references to your sub-modules. Each (blessed) reference gets saved in %modules for later use. If you are feeling appropriately paranoid, you wrap this in an eval.

    ----
    I Go Back to Sleep, Now.

    OGB