打包一个 Perl 应用程序,以便它可以在 Perl 的默认前缀之外工作

发布于 2024-10-21 18:57:34 字数 923 浏览 7 评论 0原文

我正在使用 Module::Build (尽管我在构建环境上很灵活)来打包我正在编写的一些 Perl 软件,供我工作的内部使用。它包括一些脚本和一些辅助模块。我的计划是这样你可以在构建过程中指定任何你想要的前缀(即 perl 默认 @INC 之外的东西),并且构建的脚本应该仍然能够找到它们的帮助程序模块没有任何问题。

我想这样做是因为我想使用“Encap”在内部分发该软件,这是一个打包工具,默认情况下不能安装 /usr/local 之外的任何内容,并且位于我们的 RedHat 上。默认情况下,perl 不会在 /usr/local/lib 中搜索模块。

这让我有可能告诉用户每次想要运行应用程序时手动将 PERL5LIB 设置为 /usr/local/lib,或者对构建系统进行一些智能操作来获得它指定 --prefix 后,修改每个脚本的 use lib 行。

现在,我只是在每个脚本中手动设置 use lib 直接指向 /usr/local/lib ,但我不太喜欢这种解决方案。主要是因为测试过程:我想在测试期间覆盖 @INC ,以便它首先使用我的工作目录作为 perl 模块,但是在构建后,应该从 @ 中删除工作目录INC 并替换为用户指定的前缀。但也因为我希望这个软件可以安装到任意位置(例如 NFS 上的某个小岛,有自己的 bin/ 和 lib/ 目录)并且仍然可以正常工作。

问题

Module::Build 是否允许我在构建步骤期间摆弄脚本的 use lib 行?我注意到 MakeMaker 有一个 pm_filter 选项,允许您指定搜索和替换,可以在构建时任意修改 .pm 文件,但这仅适用于 .pm 文件,不适用于脚本。 Module::Build 应该更灵活,但我淹没在文档中试图找出你在哪里指定它。

I'm using Module::Build (although I'm flexible on build environments) to package up some perl software I'm writing for internal use where I work. It includes a handful of scripts, and some helper modules. My plan is to make it so you can specify a prefix of whatever you want (ie. something outside of perl's default @INC) during the build process and the built scripts should still be able to find their helper modules without any problems.

I want to do this because I want to distribute this software internally with "Encap", which is a packaging tool that by default can not install anything outside of /usr/local, and being on RedHat, our perl does not search /usr/local/lib for modules by default.

This leaves me with the prospect of either telling the user to manually set PERL5LIB to /usr/local/lib every time they want to run the app, or to do something intelligent with the build system to have it fiddle with each script's use lib line after a --prefix is specified.

Right now I'm just setting use lib to point straight to /usr/local/lib manually in each of my scripts, but I'm not really liking that as a solution. Chiefly because of the testing process: I want to override @INC during testing so that it uses my working directory first for perl modules, but upon being built, the working directory should be removed from @INC and replaced with the user's specified prefix. But also because I would like this software to be installed to arbitrary locations (such as its own little island somewhere on NFS with its own bin/ and lib/ dirs) and still work without issue.

The question:

Can Module::Build allow me to fiddle with my scripts' use lib lines during build steps? I notice MakeMaker has a pm_filter option that lets you specify a search-and-replace that can arbitrarily modify your .pm files as they're being built, but that only works with .pm files, not scripts. Module::Build is supposed to be more flexible but I'm drowning in the documentation trying to figure out where you'd specify that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2024-10-28 18:57:34
>>> daxim@champion:/tmp/Foo-Bar$ tree
.
├── bin
│   └── foobar
├── Build.PL
├── inc
│   └── Local
│       └── Module
│           └── Build
│               └── Fnord.pm
└── lib
    └── Foo
        └── Bar.pm

7 directories, 4 files

>>> daxim@champion:/tmp/Foo-Bar$ cat bin/foobar
use lib "DUMMY";
use Foo::Bar;
print "It works!\n";

>>> daxim@champion:/tmp/Foo-Bar$ cat Build.PL
use lib 'inc';
use Local::Module::Build::Fnord;

my $build = Local::Module::Build::Fnord->new(
    module_name => 'Foo::Bar',
    license     => 'restricted',
);
$build->add_build_element('bin');
$build->create_build_script;

>>> daxim@champion:/tmp/Foo-Bar$ cat inc/Local/Module/Build/Fnord.pm
package Local::Module::Build::Fnord;
use parent 'Module::Build';
sub process_bin_files {
    my ($self) = @_;
    my $lib = $self->install_base . '/lib/perl5';
    system "chmod u+w blib/script/*";
    my $call = qq($^X -p -i -e's[use lib "DUMMY";][use lib "$lib";]' blib/script/*);
    print "$call\n";
    system $call;
};
1;

>>> daxim@champion:/tmp/Foo-Bar$ cat lib/Foo/Bar.pm
package Foo::Bar;
1;

>>> daxim@champion:/tmp/Foo-Bar$ perl Build.PL --install_base=/tmp/usr/local
⋮

>>> daxim@champion:/tmp/Foo-Bar$ ./Build install
Building Foo-Bar
/home/daxim/local/bin/perl -p -i -e's[use lib "DUMMY";][use lib "/tmp/usr/local/lib/perl5";]' blib/script/*
Installing /tmp/usr/local/lib/perl5/Foo/Bar.pm
Installing /tmp/usr/local/bin/foobar

>>> daxim@champion:/tmp/Foo-Bar$ cat blib/script/foobar
use lib "/tmp/usr/local/lib/perl5";
use Foo::Bar;
print "It works!\n";

>>> daxim@champion:/tmp/Foo-Bar$ cd /tmp/usr/local/bin/

>>> daxim@champion:/tmp/usr/local/bin$ perl foobar
It works!
>>> daxim@champion:/tmp/Foo-Bar$ tree
.
├── bin
│   └── foobar
├── Build.PL
├── inc
│   └── Local
│       └── Module
│           └── Build
│               └── Fnord.pm
└── lib
    └── Foo
        └── Bar.pm

7 directories, 4 files

>>> daxim@champion:/tmp/Foo-Bar$ cat bin/foobar
use lib "DUMMY";
use Foo::Bar;
print "It works!\n";

>>> daxim@champion:/tmp/Foo-Bar$ cat Build.PL
use lib 'inc';
use Local::Module::Build::Fnord;

my $build = Local::Module::Build::Fnord->new(
    module_name => 'Foo::Bar',
    license     => 'restricted',
);
$build->add_build_element('bin');
$build->create_build_script;

>>> daxim@champion:/tmp/Foo-Bar$ cat inc/Local/Module/Build/Fnord.pm
package Local::Module::Build::Fnord;
use parent 'Module::Build';
sub process_bin_files {
    my ($self) = @_;
    my $lib = $self->install_base . '/lib/perl5';
    system "chmod u+w blib/script/*";
    my $call = qq($^X -p -i -e's[use lib "DUMMY";][use lib "$lib";]' blib/script/*);
    print "$call\n";
    system $call;
};
1;

>>> daxim@champion:/tmp/Foo-Bar$ cat lib/Foo/Bar.pm
package Foo::Bar;
1;

>>> daxim@champion:/tmp/Foo-Bar$ perl Build.PL --install_base=/tmp/usr/local
⋮

>>> daxim@champion:/tmp/Foo-Bar$ ./Build install
Building Foo-Bar
/home/daxim/local/bin/perl -p -i -e's[use lib "DUMMY";][use lib "/tmp/usr/local/lib/perl5";]' blib/script/*
Installing /tmp/usr/local/lib/perl5/Foo/Bar.pm
Installing /tmp/usr/local/bin/foobar

>>> daxim@champion:/tmp/Foo-Bar$ cat blib/script/foobar
use lib "/tmp/usr/local/lib/perl5";
use Foo::Bar;
print "It works!\n";

>>> daxim@champion:/tmp/Foo-Bar$ cd /tmp/usr/local/bin/

>>> daxim@champion:/tmp/usr/local/bin$ perl foobar
It works!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文