如何将 Perl 应用程序作为单个文件分发?

发布于 2024-08-04 18:31:20 字数 382 浏览 5 评论 0原文

我有一个 Perl 脚本 (foo.pl),它使用 require 机制从同一目录加载 Foo.pm:

require "./Foo.pm";
...
my $foo = new Foo::Bar;

Foo.pm 遵循标准模块格式:

package Foo::Bar;
...
1;

而不是将我的应用程序分发为两个文件(foo.pl 和 Foo.pm)。 pm) 我只想分发一个文件。更具体地说,我想让 Foo.pm 成为 foo.pl 脚本的一部分。

我该如何实现这一目标?

简单地合并两个文件(cat foo.pl Foo.pm > foo2.pl)的简单方法不起作用。

I have a Perl script (foo.pl) that loads Foo.pm from the same directory using the require mechanism:

require "./Foo.pm";
...
my $foo = new Foo::Bar;

The Foo.pm adheres to the standard module format:

package Foo::Bar;
...
1;

Rather than distributing my application as two files (foo.pl and Foo.pm) I'd like to distribute only one file. More specifically I'd like to make Foo.pm part of the foo.pl script.

How do I achieve that?

The trivial approach of simply merging the two files (cat foo.pl Foo.pm > foo2.pl) does not work.

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

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

发布评论

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

评论(5

别在捏我脸啦 2024-08-11 18:31:20

如果您有兴趣将 Perl 脚本打包成二进制文件,并包含它所依赖的所有模块,您可以使用 PAR 打包器

pp -o binary_name foo.pl

If you're interested in packing up your Perl script into a binary with all the modules it depends upon included, you can use PAR Packager:

pp -o binary_name foo.pl
狼性发作 2024-08-11 18:31:20

一个文件可以包含多个包。把你的类放在前面,然后是主脚本:

package Foo::Bar;

sub new { 
  my $class = shift;
  return bless {}, $class;
}

#...

package main;

my $foo = Foo::Bar->new();
print ref $foo;  # Foo::Bar

A file can contain multiple packages. Put your class first, followed by the main script:

package Foo::Bar;

sub new { 
  my $class = shift;
  return bless {}, $class;
}

#...

package main;

my $foo = Foo::Bar->new();
print ref $foo;  # Foo::Bar
来日方长 2024-08-11 18:31:20

您的代码不起作用(尽管说明您收到的错误消息会很有帮助),因为您在定义 Foo::Bar 之前尝试使用它。
试试这个:

use strict;
use warnings;
my $foo = Foo::Bar->new();
# more code...

# end code

# begin definitions
BEGIN {
    package Foo::Bar;
    use strict;
    use warnings;
    # definitions...
    1;

    package Foo::Baz;
    # more stuff, if you need to define another class
}

补充:

Your code did not work (although it would have been helpful to state the error message(s) that you received) because you attempted to use Foo::Bar before it had been defined.
Try this:

use strict;
use warnings;
my $foo = Foo::Bar->new();
# more code...

# end code

# begin definitions
BEGIN {
    package Foo::Bar;
    use strict;
    use warnings;
    # definitions...
    1;

    package Foo::Baz;
    # more stuff, if you need to define another class
}

Additions:

你又不是我 2024-08-11 18:31:20

总体方案是将您的“要求...”替换为您需要的内容。还有更多的事情(可能需要 BEGIN { }),而且我不太确定涉及什么。当然,您希望使其自动化。

这是一种替代方法:生成一个可执行文件,其中使用 PAR/pp

The overall scheme would be to replace your "require ..." with the contents of what you're requiring. There's more to it than that (BEGIN { } may be needed), and I'm not exactly sure what's involved. For sure, you'd want to automate it.

Here's an alternative: generate a single executable file where modules you depend on are packed inside it using PAR/pp

鹤仙姿 2024-08-11 18:31:20

您已经得到了一些很好的答案。此外,还可以制作一个可以直接作为脚本运行的模块。

package Foo;

__PACKAGE__->run(@ARGV) unless caller();

sub run {
    # Do stuff here if you are running the Foo.pm as
    # a script rather than using it as a module.
}

有关更多详细信息,请参阅 brian d foy 的脚本如何成为模块

You've already got a few good answers. In addition, it is possible to make a module than can be run directly as a script.

package Foo;

__PACKAGE__->run(@ARGV) unless caller();

sub run {
    # Do stuff here if you are running the Foo.pm as
    # a script rather than using it as a module.
}

For additional details, see brian d foy's How a Script Becomes a Module.

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