Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Automatic module installation

by Juerd (Abbot)
on May 23, 2002 at 08:03 UTC ( #168693=CUFP: print w/replies, xml ) Need Help??

Let's assume you have a recent Perl and CPANPLUS installed. You want your brand new script to automatically install any modules that are not found. Well, maybe you don't. I'm sure I don't, but I couldn't resist the temptation and made The module name describes exactly what it does, or... no it doesn't... but I needed a name.

My first try had a $SIG{__DIE__} handler and die "Try again :)\n", but it still didn't look nice. I did this because you cannot resurrect scripts. Kane had the idea of using exec($^X, $0, @ARGV) instead, which worked perfectly. But then, lathos suggested using a code ref in @INC. With the help of this node, I created this:

use strict; use CPANPLUS; push @INC, sub { my ($code, $file) = @_; my $module = $file; $module =~ s!/!::!g; $module =~ s!\.pm$!!g; install($module); for (@INC) { next if ref; if (-e "$_/$file" and -r _) { open my $fh, '<', "$_/$file" or die $!; return $fh; } } return undef; };
Please note that real programmers don't use CPANPLUS but CPANPLUS::Backend, and use the great backend. I was lazy and used CPANPLUS with the simple install() function, but that is bad.

Example script:

use Blah; use Acme::Time::Baby; print babytime(), "\n";

Example output:

3;0 root@ouranos:/home/juerd/modtest# perl Checking if your kit is complete... Looks good Writing Makefile for Acme::Time::Baby PERL_DL_NONLAZY=1 /usr/bin/perl -Iblib/arch -Iblib/lib -I/usr/lib/perl +/5.6.1 -I/ usr/share/perl/5.6.1 -e 'use Test::Harness qw(&runtests $verbose); $ve +rbose=(eva l(chr(36).q(|=1))-1); runtests @ARGV;' t/*.t t/00_en..........ok t/01_du..........ok t/02_custom......ok t/03_noimport....ok t/04_chef........ok t/05_warez.......ok t/06_de..........ok t/07_fr..........ok t/08_no..........ok t/09_it..........ok All tests successful. Files=10, Tests=12972, 6 wallclock secs ( 2.33 cusr + 0.17 csys = 2 +.50 CPU) Installing of Acme::Time::Baby succesfull The big hand is on the eight and the little hand is on the ten 3;0 root@ouranos:/home/juerd/modtest# perl The big hand is on the nine and the little hand is on the ten

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Automatic module installation
by Corion (Pope) on May 23, 2002 at 09:11 UTC

    Abigail once did something similar in Re: The Future - Managing Modules with The::Net, except that Abigail didn't use CPANPLUS and that the module is not available on CPAN for reasons unknown to me. So a possible name for your module might either be The::Net indeed, as The::Net might be unclaimed at the moment (ask Abigail about this :-) ) or The::Net::CPANPLUS, as that is what you do.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

      Abigail once did something similar in Re: The Future - Managing Modules

      I knew that already. That node helped me to understand subrefs in @INC.

      And no, this isn't ever going into CPAN. I'm not even going to use it. Just wanted it to be possible :)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.

      Not available on CPAN? So, what's in then? You won't find it when searching because 'The' isn't a registered namespace, and the search functionality isn't checking what has really been uploaded.

      Note that The::Net doesn't install the module - it will fetch it each time.

        Not available on CPAN?

        Sigh. Please do learn the difference between "this" and "that".
        This module (the one we're discussing named will not enter CPAN. That module (the other, slightly off-topic one named The_Net) already has.

        Update - My mistake, as described by the anonymous reply.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://168693]
Approved by crazyinsomniac
Front-paged by jmcnamara
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2018-09-21 15:22 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (187 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!