1

I have homemade #Perl libraries that I'm moving from machine to machine, and the paths are not the same from place to place. Specifically, on the old machines, they existed in /home/foo/lib/, while they're moving to /group/bar/apps/lib on the new machines, and I now want to have them in something like /home/me/dev/Tools/lib.

What we did was have multiple use lib lines. /home/foo isn't available on the new machine, and /group/bar isn't a directory on the old machine, so when it sees this --

use lib '/home/foo/lib/' ;
use lib '/group/bar/apps/lib' ;
use Tools::Foo ;

-- everything is fine.

The problem is, they link to each other, and I'd rather not have something in /home/me/dev/Tools/lib load a program from /group/bar/apps/lib, and when I move this stuff to production, I don't want to have anything pointing back to ~/me/dev. Preferrably, I would want to not have to modify the code when I move it into production, so that, when everything is deployed, diff /group/bar/apps/lib/Tools/Foo.pm /home/me/dev/Tools/lib/Tools/Foo.pm would be empty.

So, how do I set things for multiple conditional library locations?

4

2 回答 2

11

选项:

  1. 正确安装模块。

  2. 相对于脚本放置模块

    use FindBin qw( $RealBin );
    use lib "$RealBin/../lib";  # Or whatever.
    
  3. 使用环境变量PERL5LIB而不是use lib.

  4. 可以放置语句sitecustomize.pl(如果在构建sitecustomize.pl时启用了对的支持perl)。

于 2013-08-27T13:50:03.423 回答
2

使用以下编译指示

package lib_first_of;

use lib ();
use strict;
use warnings;

use Carp;

sub import {
  foreach my $path (@_) {
    if (-d $path) {
      lib->import($path);
      return 1;
    }
  }

  croak "$0: no suitable library path found";
}

1;

让您的主程序具有以下形式

#! /usr/bin/env perl

use strict;
use warnings;

use lib_first_of (
  "/home/foo/lib",
  "/group/bar/apps/lib",
);

use MyModule;

print "done.\n";

如果两个路径都不存在,则程序将失败并出现类似的错误

我的程序:在我的程序第 7 行找不到合适的库路径
BEGIN failed——编译在我的程序第 9 行中止。
于 2013-08-27T18:26:41.797 回答