Jenda Krynicky ha scritto:
Date sent:              Thu, 28 Aug 2008 17:25:16 +0200
From:                   sormariano <[EMAIL PROTECTED]>
To:                     beginners@perl.org
Subject:                Check for modules presence

Hello to everybody,

I made a script that uses some modules.
Obviously if one of the modules is not installed, the script doesn't start.
I'd like to have the possibility to notify to the user which are the modules he still requires and that should have to be installed.. I think I could wrote an "install script" that checks the modules presence and if all it's ok, it starts the main script.
So here I have several questions:
1) is this a good way to proceed ?
2) how could I check the module presence ?
3) Can I start the main script with a 'system mainscript.pl'


Put something like this on top of the script:

BEGIN {
  push @INC, sub {print STDERR "Still looking for $_[1]\n";exit;};
}

I tried this and I got the following output

Still looking for Encode/ConfigLocal.pm

also if the modules were all installed.
Moreover, it works fine if I insert a non existent module: e.g. I put as first required module:
use Tk2;
and, correctly, your sulution says:
Still looking for Tk2.pm

Could you explain to me what exactly your code does and why he looks for Encode/ConfigLocal.pm ?

About a check script as I said before, I wrote something like this
( please, don't blame me about the way to write perl: I'm a C programmer in love with Perl since 2 years only :-D )

#!/usr/bin/perl
use File::Copy;
use Test;

use strict;

$| = 1;

BEGIN { plan tests => 12 }

my @REQUIRED_MODULES = ( 'Tk',
                        'DBD::SQLite',
                        'MP3::Info',
                        'Archive::Rar',
                        'Archive::Zip' );
my @NOTFOUND_MODULES = ();

my $warn = 0;
my $str;

foreach my $module ( @REQUIRED_MODULES ) {
   eval "require $module";
   ok($@, '', "Error loading $module");

   if ( $@ ne '' ) {
       push @NOTFOUND_MODULES, $module;
       $warn++;
   }
}

foreach my $module ( @NOTFOUND_MODULES ) {
   $str = $str . "Please, install [$module] module\n";
}

if ( not $warn ) {
   system ("./mainscript.pl");
} else {
   $str = $str . "\nCan't run mainscript\n";
   system ( `kdialog --sorry "$str"`);
}


It works but I prefer to write all into 1 script.

Francesco

You can of course not only inform the user, but also attempt to install the module for him or something like that.

See the "require" section in perldoc perlfunc, the "hooks".

HTH, Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to