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

Dear Pearls,

I'm trying to have a small sub routine which will (expected to run on a linux box preinstalled with basic perl) go thru and check for certain perl modules, if it doesn't find (some of or all the modules), I want the same to be installed from this sub routine itself,

Here's what I've got:

for my $module (@perl_modules) { eval "$module"; if ($@) { $result.="$module\n"; } } if ($result eq '') { print qq! All Required Perl Modules found\!\n !; } else { print qq! \nErr: The following perl modules needs to be insta +lled:\n $result\n!; &installModules($result); } sub installModules { #need pointers # }
Any help will be highly appreciated

Oyster

edited: Thu Sep 11 14:27:19 2003 by jeffa - s/<br>//g; added code tags

Replies are listed 'Best First'.
Re: Install modules thru a script
by Abigail-II (Bishop) on Sep 11, 2003 at 11:39 UTC
    use CPAN; foreach my $module (@modules) { CPAN::Shell -> expand (Module => $module) -> install }

    Abigail

Re: Install modules thru a script
by tachyon (Chancellor) on Sep 11, 2003 at 11:37 UTC

    If you have a stock load of modules I would be inclined to tar the whole lot up. Then just untar them. Iterate through the dirs and shell out to install them. The advantage of this approach is that you will get all the versions that you have presumably been using in development.

    use Cwd; my $cwd = cwd(); # untargz everything in current dir for my $tgz( glob("$cwd/*.tar.gz") ) { print `tar -xzf $tgz`; } opendir DIR, $cwd or die $!; my @dirs = grep{ -d $_ and ! m/^./ } readdir DIR; closedir DIR; for my $dir ( @dirs ) { chdir "$cwd/$dir"; print `perl Makefile.PL && make && make test && make install`; }

    If you are on *nix you could open a pipe to the shell so you can see what is happening during the install....

    Here is some code I wrote a while back the from memory works OK. It does not check for versions which may be important. For example only the latest CGI.pm and CGI::Simple support P3P, the latest DBI has some useful feature, etc, etc:

    sub check_modules { my @mod_list = @_; print "\nI will check to see that you have all the required Perl m +odules...\n\n"; my @not_installed; for my $module ( @mod_list ) { eval "require $module"; printf "%-20s", $module; if ( $@ ) { print "Not Installed\n"; push @not_installed, $module; } else { print "OK\n"; } } if ( @not_installed ) { print " The following module(s) are not installed: @not_installed\n Would you like me to try to install them (y/n)? "; if ( <> =~ m/y/i ) { install_modules( @not_installed ); print " If everything seemed to go OK we can now continue. If the automatic install failed you will need to exit now and install the module(s) by hand. Note the names of the missing modules and see the file INSTALLING_MODULES for detailed instructions. Continue (y/n)? "; exit if <> =~ /n/i; } } } sub install_modules { my @modules = @_; print "\nI will now try to install module(s) @modules\n\n"; if ( $OS =~m/MSWin32/ ) { print " You are using $OS so I can try to do the install with ppm (ActiveState's Perl Package Manager) or CPAN. What shall we try? Hint: Try ppm first p = ppm, c = CPAN (p/c)? "; <> =~ m/p/i ? ppm_install( @modules ) : cpan_install ( 'win32' +, @modules ); } else { print " If you are using Debian I can use either apt-get or CPAN. What shall w +e try? c = CPAN, a = apt-get (c/a)? "; <> =~ m/a/i ? apt_get_install( @modules ) : cpan_install ( '', + @modules ); } } sub cpan_install { my ($win32, @modules) = @_; my $quote = $win32 ? '"' : "'"; print " I will now try to install the missing modules using the CPAN shell. If you have not used this before you will need to answer a number of initialization questions. You can generally just accept the defaults and it will work. Unfortunately if you have not used it before and try + to use it here it will hang invisibly waiting for your input, so you w +ill need to run it from the command line using the command below....\n\n"; for my $module ( @modules ) { my $cmd = 'perl -MCPAN -e ' . $quote . 'install '. $module . $ +quote; print "Trying to install $module command line $cmd\nPlease be +patient.....\n"; print `$cmd`; } } sub ppm_install { my @modules = @_; print " I will now try to install the missing modules using the ppm shell..... +\n\n"; for my $module ( @modules ) { my $cmd = qq!ppm install "$module"!; print "Trying to install $module, command line $cmd\nPlease be + patient.....\n"; print `$cmd`; } } sub apt_get_install { my @modules = @_; print " I will now try to install the missing module with apt-get.....\n\n"; for my $module ( @modules ) { my $cmd; $cmd = 'apt-get install libcgi-perl' if $module eq + 'CGI'; $cmd = 'apt-get install libdbi-perl' if $module eq + 'DBI'; $cmd = 'apt-get install libmail-pop3client-perl' if $module eq + 'Mail::POP3Client'; $cmd = 'apt-get install libmime-perl' if $module eq + 'MIME::Base64'; $cmd = 'apt-get install libmime-lite-perl' if $module eq + 'MIME::Lite'; $cmd = 'apt-get install libcrypt-blowfish-perl' if $module eq + 'Crypt::Blowfish'; $cmd = 'apt-get install libcrypt-cbc-perl' if $module eq + 'Crypt::CBC'; $cmd = 'apt-get install libnet-perl' if $module eq + 'Net::SMTP'; $cmd = 'apt-get install libtext-perl' if $module eq + 'Text::Wrap'; print "Trying to install $module, command line $cmd\nPlease be + patient.....\n"; print `$cmd`; } }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Install modules thru a script
by naChoZ (Curate) on Sep 11, 2003 at 12:12 UTC
      Hey Pearls,

      Mighty Thanks to you all for the interesting replies ...
      it's working perfectly. Out of respect to the other 2 pearls I'll not mention the one I used.

      May I ask ABBA to bless you with more wisdom for ALL things in life. :)

      Oyster