12

I wrote a few Perl libraries(.pm) and Perlscripts(.pl) on Ubuntu and I need to distribute them to my colleagues in my office. Those scripts and libraries require third party libraries. I want to make it very simple.

Infomation about me.

  • I know how to create a Perl script.
  • I know how to create bash file.
  • I have no experience in creating MAKE file, rpm, Deb etc.
Konerak
  • 39,272
  • 12
  • 98
  • 118
Jessada Thutkawkorapin
  • 1,336
  • 3
  • 16
  • 32
  • If you colleagues all have Ubuntu (preferably same version) and there are enough of them, it could be worthwhile to learn how to build a `*.deb` package. – Basile Starynkevitch Mar 05 '12 at 12:13
  • @BasileStarynkevitch, I don't want to make it specific. They are either linux or Mac users – Jessada Thutkawkorapin Mar 05 '12 at 12:15
  • Have you read [Module::Build](http://search.cpan.org/dist/Module-Build/lib/Module/Build.pm)? – Konerak Mar 05 '12 at 12:16
  • @Konerak, I haven't tried it yet. Is it the simplest way? At first, I thought rpm is the simplest but I stuck with many different version of 'howto'. So I decided to ask experts here. – Jessada Thutkawkorapin Mar 05 '12 at 12:24
  • @Konerak, actually, I did pass that kind of page for several times while I was trying to learn this stuffs on my own before I asked this question. My main problem is I only saw Standard process for building & installing modules: perl Build.PL ./Build ./Build test ./Build install but don't know where to create this make file and how to let it know my script – Jessada Thutkawkorapin Mar 05 '12 at 12:38
  • @JessadaThutkawkorapin - Creating the makefile is what `Makefile.PL` does. –  Mar 05 '12 at 12:45

2 Answers2

15

I'd recommend using Module::Starter to set up a template for each module. Once it's installed, you can call module-starter from the command line, eg:

module-starter --module=My::Module --author="Jessada Thutkawkorapin" --email=your@email.com

or, if you want a distribution with multiple modules:

module-starter --distro=Foo --module=Foo,Foo::Bar,Foo::Baz --author="Jessada Thutkawkorapin" --email=your@email.com

Then, just overwrite the .pm files with your modules, include any unit tests that you want to run (the default tests basically check the syntax of the module along with the pod syntax). A basic installation of the modules is then done via

perl Makefile.PL
make
make test
make install

(technically, make test is optional, but it's highly recommended).

Now, if these modules rely on other CPAN modules, then you can use the module CPAN to install them, eg:

use strict;
use warnings;
use CPAN;

#populate however you'd like, either hard-coded, read from a file, etc.
my @modules_to_install=(); 

foreach(@modules_to_install)
{
  CPAN::Shell->install($_);
}

So, you can distribute a zip/tarball/etc with the folders and files that module-starter started (and that you modified) along with the above script to install any CPAN dependencies, and call it, say, cpan_install.pl. If you want, you can then wrap everything up in a final script called, say, install.pl that does all of these things.

  • Prabably, my main problem here is that how and where can I specific the location of my script? All my script are under /bin and my .pm are under /bin/lib – Jessada Thutkawkorapin Mar 05 '12 at 12:35
  • 2
    Well, for each module (and for the distro, if you use the `--distro` option), `module-starter` includes `lib` and `t` directories, along with some other boilerplate that you can mostly ignore. Once the modules are installed (via `perl Makefile.PL;make;make test;make install`), then they're installed into the `site_perl` subdirectory of whatever Perl distribution you're using. –  Mar 05 '12 at 12:39
  • 8
    Downvote for [invoking CPAN yourself to install missing modules](http://stackoverflow.com/q/8183293). Instead declare the dependencies with [`PREREQ_PM`](https://metacpan.org/module/ExtUtils::MakeMaker#PREREQ_PM) so they end up in the distro meta file, also see examples in http://stackoverflow.com/a/7664993 and http://stackoverflow.com/a/2606677. – daxim Mar 05 '12 at 14:21
2

My usual method these days is to simply copy a Build.PL file from one of my existing distributions, and go from there. Obviously that's not a useful strategy for the first one, but CPAN is full of thousands of these files.

For a simple single-.pm file pure-perl distribution it's probably easiest to start by copying someone else's Build.PL file and edit the fields as appropriate for your case. Likely all you should need to change are the module_name and requires fields.

Here's a simple one of mine you can steal^W be inspired by:

use strict;
use warnings;

use Module::Build;

my $build = Module::Build->new(
   module_name => 'Your::Name::Here',
   requires => {
      'Your::Requirements::Here' => '1.23',
   },
   build_requires => {
      'Test::More' => 0,
   },
   license => 'perl',
   create_makefile_pl => 'traditional',
   create_license => 1,
   create_readme  => 1,
);

$build->create_build_script;

These fields, in order, mean:

  • The name of the primary module in the distribution - this is where the name of the distribution itself is taken from, as well as its version and abstract summary
  • Other modules that this distribution will depend on to build or run
  • Other modules that this distribution will depend on to build, but won't be required once it's installed (typically this will be Test:: modules or other build tools)
  • The licencing terms applied to the distribution
  • Create a legacy Makefile.PL for older CPAN clients that do not understand the Build.PL protocol
  • Create a LICENSE file automatically from the declared licencing terms above
  • Create a README file automatically by turning the main module's POD documentation into plaintext
LeoNerd
  • 8,344
  • 1
  • 29
  • 36