Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Perl创建条件库?_Perl - Fatal编程技术网

如何使用Perl创建条件库?

如何使用Perl创建条件库?,perl,Perl,我有自制的Perl库,我正在从一台机器移动到另一台机器,而且路径在不同的地方不一样。具体地说,在旧机器上,它们存在于/home/foo/lib/中,而在新机器上它们移动到/group/bar/apps/lib,我现在希望它们存在于类似于/home/me/dev/Tools/lib的东西中 我们所做的是使用多个库行/home/foo在新机器上不可用,/group/bar在旧机器上不是目录,因此当它看到这一点时-- --一切都很好 问题是,它们相互链接,我不希望在/home/me/dev/Tools

我有自制的Perl库,我正在从一台机器移动到另一台机器,而且路径在不同的地方不一样。具体地说,在旧机器上,它们存在于/home/foo/lib/中,而在新机器上它们移动到/group/bar/apps/lib,我现在希望它们存在于类似于/home/me/dev/Tools/lib的东西中

我们所做的是使用多个库行/home/foo在新机器上不可用,/group/bar在旧机器上不是目录,因此当它看到这一点时--

--一切都很好

问题是,它们相互链接,我不希望在/home/me/dev/Tools/lib中有任何东西从/group/bar/apps/lib加载程序,当我将这些东西移到生产环境中时,我不希望有任何东西指向~/me/dev。更希望的是,当我将代码移到生产环境中时,我不需要修改代码,部署所有内容时,
diff/group/bar/apps/lib/Tools/Foo.pm/home/me/dev/Tools/lib/Tools/Foo.pm
将为空

那么,如何为多个条件库位置设置内容?

选项:

  • 正确安装模块

  • 相对于脚本放置模块

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

  • 这些语句可以放在
    sitecustomize.pl
    中(如果在构建
    perl
    时启用了对
    sitecustomize.pl
    的支持)

  • 选项:

  • 正确安装模块

  • 相对于脚本放置模块

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

  • 这些语句可以放在
    sitecustomize.pl
    中(如果在构建
    perl
    时启用了对
    sitecustomize.pl
    的支持)


  • 使用以下pragma

    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";
    
    如果两条路径都不存在,程序将失败并出现错误

    my-program: no suitable library path found at my-program line 7 BEGIN failed--compilation aborted at my-program line 9. 我的程序:在我的程序第7行找不到合适的库路径
    BEGIN失败--编译在程序第9行中止。使用以下pragma

    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";
    
    如果两条路径都不存在,程序将失败并出现错误

    my-program: no suitable library path found at my-program line 7 BEGIN failed--compilation aborted at my-program line 9. 我的程序:在我的程序第7行找不到合适的库路径
    BEGIN失败--编译在我的程序第9行中止。+1
    sitecustomize.pl
    也可以用于类似效果,具体取决于生成选项。@pilcrow,谢谢,将其添加到了答案中。+1
    sitecustomize.pl
    也可以用于类似效果,具体取决于生成选项。@pilcrow,谢谢,将其添加到了答案中。