开发者

How to dynamically set the directory for user-specified libraries

I have some fairly complex libraries that interconnect wi开发者_如何学Cth each other to do some work, and while they mostly run on our servers, they're connecting to some high-performance servers to crunch numbers.

On our servers, the line is ...

 use lib '/home/ourgroup/lib' ;
 use HomeGrown::Code ':all' ;

On the high-performance cluster, it's more like ...

use lib '/scratch/ourgroup/lib' ;
use HomeGrown::Code ':all' ;

For the programs that use the modules, that's a reasonably easy thing to set, but I would like to not have to make server-specific changes in the code base. I'd rather be able to copy the directory as-is. So, how do I tell my modules to use my library dir without hard-coding it like this?


Usually you do this by having the PERL5LIB environment variable set differently on different machines. Yes, it's not a pure-Perl solution, but it has to be done only once per server, not once per deployment.


Here's what we're going with.

use lib '/home/ourgroup/lib' ;
use lib '/scratch/ourgroup/lib' ;

If /home/ourgroup/lib doesn't exist on one machine, so be it. If /scratch/ourgroup/lib doesn't exist on the other, so be it. It doesn't complain, so that's what we're doing.


You can also have a module that detects the environment and includes the correct directories for for you.

To use it, just do:

use VarLogRant::FindLibs; 
use Stuff;

And to write the module:

package VarLogRant::Findlibs;

sub select_lib_dirs {

    my @libs;

    push @libs, '/home/ourgroup/libs' if need_home();
    push @libs, '/scratch/ourgroup/libs' if need_scratch();

    # Any other magical logic you want.

}

# It is critical that use lib comes AFTER the functions are defined.
use lib select_lib_dirs();

1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜