newbie_coder has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

Pardon me for my out of place use of words, as I am new in programming arena.

I am working on a tool which will work from command line. I have coded different functionality in different .pm files. Some of the .pm files use CPAN modules, viz Readline, which have few dependencies as well.

I want to install the tool in other systems in such a way that it automatically installs all the required CPAN modules along with all the dependencies and then can be run on the command line with a single command henceforth.

Please help.
  • Comment on How to combine multiple perl modules to make them run from command line

Replies are listed 'Best First'.
Re: How to combine multiple perl modules to make them run from command line
by Tanktalus (Canon) on Nov 10, 2010 at 05:28 UTC

    The basic idea is to package it up and have your users install it just like all other modules: perl Makefile.PL; make; make test; make install. If you use, for example, Module::Install's auto_install, this will cause all the prereqs to be installed, too.

    If you want even simpler, you could write a wrapper script which downloads the tarball, runs the untar commands, and then runs perl Makefile.PL, etc., for the user. That's not too hard on unix/linux - but may be a bit more work on Windows, since Windows doesn't come with handy things like wget or tar or gzip, and requiring things like LWP kind of defeats the idea. But your script could install LWP and Archive::Tar and such before downloading your tarball by using ppm, I think, though I've never tried.

Re: How to combine multiple perl modules to make them run from command line
by Khen1950fx (Canon) on Nov 10, 2010 at 07:19 UTC
    If you have CPAN and the appropriate permissions, then you could easily use a script to install from the command line. For example, using CGI:
    #!/usr/bin/perl use strict; use warnings; use CPAN; CPAN::Shell->install('CGI');
    Now, let's say you want to install all the dependencies. Cool, but the major dependencies are mostly core and/or dual-lived. You can't install or upgrade core dependencies without reinstalling perl, so you'll need to scratch the core modules off your list. The dual-lived modules can be upgraded, so leave them on the list. Add to that list any noncore dependencies that are there, and the script is done.

    How do you find the dependencies? An easy way is to use CPAN::FindDependencies. Download it, then run this script to get a record:

    #!/usr/bin/perl use strict; use warnings; use CPAN; open STDOUT, '>', '/root/Desktop/CGIdeps'; system("cpandeps CGI"); close STDOUT;
    You can also run cpandeps CGI from the command line.

    Next, to determine which modules are core, you want to use Module::CoreList. From the command line, do corelist CGI. It'll tell you that it's core.

    Is CGI core only or dual-lived? Use App::DualLivedList. From the command line, do dual-lived CGI. It'll tell you if it's dual-lived.

    The finished script should look something like this:

    #!/usr/bin/perl use strict; use warnings; use CPAN; CPAN::Shell->install( "FCGI", "Scalar::Util", "File::Spec", "Test::Harness", "Test::More", "CGI");

      What's in core varies from one version of perl to another, so unless you also nail down *exactly* what version of perl your application is for, then you may need to be a bit clever about core modules. But only a bit clever, cos it's pretty easy really.

      For every module that you depend on, including core modules, check to see if a recent enough version of it is installed. If not, add it to the list of modules that you need to install. Many core modules (at least of those which haven't been in core since forever) are dual-lived so also exist independently on the CPAN.

      Your script will end up looking something like this ...

      #!/usr/bin/perl use strict; use warnings; use CPAN; my %needed = ( Some::Module => 1.23, # must have at least version 1.23 Other::Module => 0, # any old version will do ... ); my @need_to_install = (); foreach my $module (keys %needed) { eval "use $module;"; if($@ || eval "${module}::VERSION < $needed{$module}") { push @need_to_install, $module; next; } } CPAN::Shell->install(@need_to_install);
Re: How to combine multiple perl modules to make them run from command line
by CountZero (Bishop) on Nov 10, 2010 at 09:33 UTC
    Have a look at PAR, PAR::Packer and the pp utility. It will probably do exactly what you require.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to combine multiple perl modules to make them run from command line
by marto (Cardinal) on Nov 10, 2010 at 09:36 UTC

    Pehraps you want to package your application and dependencies using PAR or pp. See http://par.perl.org for further details.

Re: How to combine multiple perl modules to make them run from command line
by siraj (Sexton) on Nov 10, 2010 at 11:20 UTC
    Kinldy refer the autobundle option of CPAN which will bundle all the install modules in the system. So that the same can the installed in any system.

    perl -MCPAN -e autobundle
    perl -MCPAN -e 'install Bundle::Snapshot*′