Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

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.

like image 601
knorv Avatar asked Sep 12 '09 18:09

knorv


2 Answers

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
like image 71
Drew Stephens Avatar answered Sep 28 '22 14:09

Drew Stephens


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
like image 25
Michael Carman Avatar answered Sep 28 '22 14:09

Michael Carman