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

Hi Monks, I'm using ActivePerl Win2000/XP v5.6.1, and I'd like to be able to locate and load user modules in a local directory. I have code very similar to this:
if (require "eval $module") { ... }
where $module is the name of the package (without the '.pm') This works, however, my program has numerous user modules, and I'd like to put them all in a 'modules' directory off the base program directory. Is there a painless way to load them from this directory without changing package names? It seems so simple, but I can't figure it out. Thanks, Michael

Replies are listed 'Best First'.
Re: locating modules in local dir
by matija (Priest) on Feb 25, 2004 at 17:12 UTC
    You could say
    use lib "/your/module/directory";
    Note that this will only work for modules with simple names, not for the ones that have the double colon (::) in the name. For those modules, you will still have to provide the appropriate subdirectory tree.
      "Note that this will only work for modules with simple names, not for the ones that have the double colon (::) in the name. For those modules, you will still have to provide the appropriate subdirectory tree."

      This is not true. For example: /your/module/directory/Special/Module.pm will be accessable using:

      use lib "/your/module/directory"; use Special::Module;

      Update: maybe I don't get your comment right, but I don't see what you mean with "provide the appropriate subdirectory tree."

      --
      b10m

      All code is usually tested, but rarely trusted.
Re: locating modules in local dir
by halley (Prior) on Feb 25, 2004 at 17:30 UTC
    If you want application-specific modules to be relative to the application's main script, combine use lib with use FindBin to get something like the following:
    use FindBin; use lib "$FindBin::Bin/lib"; use WidgetCorp::Widget; use WidgetCorp::Mangler; use Text::Distort; use Wiggle;
    Now you can structure your application's private modules thusly.
    ./widgettool.pl ./lib/Wiggle.pm ./lib/Text/Distort.pm ./lib/WidgetCorp/Widget.pm ./lib/WidgetCorp/WidgetMangler.pm
    I emphasize that this is useful for application-specific or highly customized modules. For standard stock or CPAN modules, though, it's still preferable to have these installed properly for all users of the system.

    --
    [ e d @ h a l l e y . c c ]