How can I distribute my Perl application as a single file?

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;

Foo.pm adheres to the standard module format:

package Foo::Bar;
...
1;

Instead of distributing my application as two files (foo.pl and Foo.pm), I would like to distribute only one file. In particular, I would like to make Foo.pm part of the foo.pl script.

How do I achieve this?

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

+5
source share
5 answers

( , ), 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
}

:

+3

, Perl script , , PAR Packager:

pp -o binary_name foo.pl
+5

. , script:

package Foo::Bar;

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

#...

package main;

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

"..." , . , (, BEGIN {}), , . , .

: , , , , PAR/pp

+2

. , , 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.
}

. brian d foy Script .

+2

All Articles