如何在没有自定义模块开销的情况下分发本机 Perl 脚本?

发布于 2024-08-10 22:32:56 字数 287 浏览 3 评论 0原文

如何在不强迫用户了解脚本运行所需的自定义(非 CPAN)模块的情况下分发本机(非“已编译/perl2exe/...”)Perl 脚本?

问题是用户不可避免地将脚本复制到系统上的其他位置,并将脚本从其本机环境中取出,然后它就无法再找到它需要运行的模块。

有时我只是将模块复制到实际脚本中,但我更喜欢更干净的解决方案。

更新:我最好澄清一下。我分发了一堆脚本,这些脚本恰好在后端使用类似的模块。用户了解如何运行 Perl 脚本,但与其依赖于告诉他们“不,不要移动脚本”,我宁愿简单地允许他们移动文件。阻力最小的路径。

How can someone distribute native (non-"compiled/perl2exe/...") Perl scripts without forcing users to be aware of the custom (non-CPAN) modules that the scripts needs in order to run?

The problem is users will inevitably copy the script somewhere else on the system and take the script out of its native environment and then it can no longer find the modules it needs to run.

I've sometimes settled with just copying the module into the actual script, but I'd prefer a cleaner solution.

Update: I better clarify. I distribute a bunch of scripts which happen to use similar modules in the backend. The users understand how to run Perl scripts, but rather than relying on telling them "no don't move the script" I'd prefer to simply allow them to move the files. The path of least resistence.

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

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

发布评论

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

评论(6

缘字诀 2024-08-17 22:32:56

正确的方法是告诉他们“不要这样做!”我希望他们不会期望移动 exe 文件并让程序继续工作。这没什么不同。

也就是说,有几种选择。一种是用知道真实脚本完整路径的包装器(例如 pl2bat)替换脚本。另一种方法是使用 PAR,但这需要 PAR 和/或 parl (来自 PAR::Packer ) 进行安装。

The right way is to tell them "Don't do that!" I would hope that they wouldn't expect to move an exe file and have the program continue to work. This is no different.

That said, there are a couple of alternatives. One is replacing the script with a wrapper (e.g. pl2bat) that knows the full path to the real script. Another is to use PAR, but that would require PAR and/or parl (from PAR::Packer) to be installed.

败给现实 2024-08-17 22:32:56

如果您为客户端准备的脚本需要“自定义”模块,只需打包您的模块,就像您尝试将它们上传到 cpan 一样。然后将包提供给客户端,他可以使用 cpan 实用程序来安装脚本和模块。

If a script that your prepared for a client needs "custom" modules, simply pack your modules as if you were trying to upload them to cpan. Then give the package to the client and he can use the cpan utility to install the script and the modules.

依 靠 2024-08-17 22:32:56

随脚本一起分发安装程序。安装程序需要以 root 权限运行,并将自定义模块放入标准系统位置(/usr/local/lib/perl5/site_perl 或其他位置)。

我没有尝试过这个,但是 Module::Install 在这方面看起来很有帮助。它被描述为:

独立、可扩展的 Perl 模块安装程序

Distribute an installer along with the script. The installer will need to be run with root privileges and it will put the custom modules into the standard system location (/usr/local/lib/perl5/site_perl or whatever).

I've not tried this, but Module::Install looks helpful in this regard. It's described as a:

Standalone, extensible Perl module installer

忱杏 2024-08-17 22:32:56

作为“将模块全部放在一个地方并让应用程序意识到这一点”的一种变体,它甚至可以跨多台计算机和网络工作,也许您应该查看 PAR::RepositoryPAR::Repository::Client。您只需提供一个二进制客户端可执行文件,该可执行文件连接到存储库(通过文件系统或 https)并执行用户请求的存储库提供的任意数量的程序(使用任意一组模块)。

如果有很多用户,这也有利于维护:只需更新存储库提供的软件,用户下次启动程序时将开始在其系统中使用更新的代码。

As a variant of the "put your modules all in one place and make your applications aware of it" that will even work across multiple computers and networks, maybe you should check out PAR::Repository and respectively PAR::Repository::Client. You'd just provide a single binary client executable that connects to the repository (via file system or https) and executes any of the arbitrary number of programs (using an arbitrary set of modules) provided by the repository that the user asks for.

If there are many users, this also has a benefit for maintenance: Simply update the software provided by the repository and the users will start using the updated code for their system when they next start the programs.

远昼 2024-08-17 22:32:56

如果您可以使用 NeXTSTEP 样式应用程序 捆绑包,那就太好了。由于您可能不是为使用捆绑包的平台进行开发,因此您必须凑合。

将所有支持文件放在已知位置,并将可执行文件指向这些文件以访问设置和库。最简单的方法是使用简单的安装程序。

例如,对于名为 foo 的应用,将所有必需的文件放入 /opt/xlyd_apps/foo 中,将库放入 /opt/xlyd_apps/foo/lib< /code>,/opt/xlyd_apps/foo/etc 中的配置,等等。将可执行文件放入 /opt/xlyd_apps/foo/bin 中。

重要的是确保可执行文件知道在 /opt/xlyd_apps/foo 中查找其所有优点,因此,如果客户/客户端将 foo 脚本移动到新位置安装仍然有效。

因此,虽然您无法使整个事情变得自包含和可重定位,但您已经使实际的调用脚本可重定位。

It would be really nice if you could just use a NeXTSTEP style application bundle. Since you probably aren't developing for a platform that uses bundles, you'll have to make do.

Put all support files in a known location, and point your executable at those files for access to settings and libraries. The easiest way to do this is with a simple installer.

For example, with an app called foo, put all your required files in /opt/xlyd_apps/foo, libraries in /opt/xlyd_apps/foo/lib, configuration in/opt/xlyd_apps/foo/etc, and so on. Put the executable in /opt/xlyd_apps/foo/bin.

The important thing is to make sure the executable knows to look in /opt/xlyd_apps/foo for all its goodies, so if the customer/client move the foo script to a new location the install still works.

So, while you can't make the whole thing self contained and relocatable, you have made the actual calling script relocatable.

深陷 2024-08-17 22:32:56

我实际上已经提出了自己的解决方案,我很好奇它会受到什么样的欢迎。

我编写了一个读取 Perl 脚本并查找“use/require”语句的脚本。找到它们后,它会检查该模块是否是标准库的一部分(查看 /perl5/\d+.\d+[\d.]+/ 的模块路径),然后以两种不同的方式重写 use/require 行,具体取决于用法。

如果找到require

{
    ... inline the entire module here...
}

如果找到use

BEGIN {
    ... inline the entire module here...
}

如果use导入,请立即按照上面的说明:

BEGIN { import Module ...imports seen... }

我明白这不适用于使用 XS 的模块,但我对此很满意。大多数情况下,我只需要支持纯 perl 模块。

I've actually come up with my own solution, and I'm kind of curious what kind of reception it will have.

I've written a script that reads a perl script and looks for "use/require" statements. Upon finding them it checks if the module is part of the standard library (looks at module path for /perl5/\d+.\d+[\d.]+/) and then rewrites the use/require line in two different ways depending on usage.

If require is found:

{
    ... inline the entire module here...
}

If use is found:

BEGIN {
    ... inline the entire module here...
}

If use has imports, immediately follow above with:

BEGIN { import Module ...imports seen... }

I understand this doesn't work with modules that use XS, but I was fine with this. Mostly I need to support only pure perl modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文