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

Hi all

Has anyone ever written some code that would install a perl package dependency if the corrosponding 'use' statement failed?

For example, if I've written code that relies on a perl module that is not part of the standard distribution and this code is to be distributed to many machines, how can I check that the required module is missing and auto install it - and do it without user intervention?

Thanks Perl Monks!
  • Comment on Auto install of a perl package if 'use' statement fails

Replies are listed 'Best First'.
Re: Auto install of a perl package if 'use' statement fails
by randyk (Parson) on Nov 07, 2006 at 06:09 UTC
      Thanks. This seems to be more along the lines of what I'm after.
Re: Auto install of a perl package if 'use' statement fails
by tilly (Archbishop) on Nov 07, 2006 at 06:18 UTC
    Yes. I've seen it done. I believe that Abigail did it, but I can't find the code.

    Anyways the idea is simple. If you read the documentation for require, you'll find that you can put subroutines in @INC. Then you can pull tricks like this untested code:

    use LWP::Simple qw(get); BEGIN { push @INC, sub { shift; my $file = shift; # eg "Foo/" my $content = get("$file"); if ($content) { open(my $fh, "<", \$content); return $fh; } else { return; } } }
    (This one grabs the module off of a webserver, and loads it directly. No local installation needed! With older versions of Perl you'll need IO::Scalar instead of the open trick that I used to put the contents of a scalar into a filehandle.)
      Thanks for this. I will follow up on it :-)
Re: Auto install of a perl package if 'use' statement fails
by Old_Gray_Bear (Bishop) on Nov 07, 2006 at 06:27 UTC
    I also am afraid of 'automatic installation' tools. Like jbruger I prefer to exercise (a modicum) of control over what gets installed on my machines. It's not that I don't trust my User community, I can't. I am the 'responsible party' when the SEC auditors come through, and I don't want to have to explain what package Xyz::IOwn::TheWorld is doing on a (supposedly) secure server that happens to be handling credit-card data. I am willing to install what the user wants/needs (within reason), when asked, but I do need to have a piece of paper for the files.

    And the other side of that coin is the tyographical error problem: The number of times I type "use warnigns;" is legion. With 'auto-install of package on use failure', then every time I typoed, Perl would have to take time out to search CPAN, determine that the module didn't exist, and report back; I'd get a lot grayer waiting. I'd be sorely tempted to build my own pragmas (with the appropriate mis-spelled name) to get around it. That would get to be both a maintenance headache and a distribution nightmare. I much prefer the current polite error message followed by a quick exit, stage left.

    I Go Back to Sleep, Now.


Re: Auto install of a perl package if 'use' statement fails
by syphilis (Archbishop) on Nov 07, 2006 at 05:53 UTC
Re: Auto install of a perl package if 'use' statement fails
by jbrugger (Parson) on Nov 07, 2006 at 05:49 UTC
    To be honest, i would not want that kind of behavior. I, and only i am to decide if a new package is installed on my machine.
    Using a good documentation along with your program, and anyone familiar using cpan should be able to deal with it.
    If only by the fact that i rather use the deb / rpm packages (if they exist) in the standard linux distribution i use, than a self compiled one.
    All system upgrades take care of new packages as well then for me.
    Don't mess with auto installation.
    For perl-only packages, well, just put them in your program library.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      This program is to operate in a build farm which has approx 200 Windows and Linux machines - so it multi-platform as well. I'd rather have a 'pull' mechanism over a 'push' mechanism so the correct perl modules get installed only on the required machines on demand.

Re: Auto install of a perl package if 'use' statement fails
by Anonymous Monk on Nov 07, 2006 at 06:43 UTC
    you could use an eval to test the existance of a particular module.
    use CPAN; eval "use My::Module" or do { CPAN::install("My::Module"); # or your code to install your custom module };
Re: Auto install of a perl package if 'use' statement fails
by idsfa (Vicar) on Nov 07, 2006 at 14:22 UTC

    See, for example, Remote Module "use" and the responses for a couple of solutions.

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon