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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With