5

I have made a script to intimate the admin the list of module need to be installed on machine .

I am trying to check wheather the module installed by underbelow code. The odd thing is that it is showing even installed module in the machine as not installed

    #!/usr/bin/perl -w
    my @module_list =('Smart::Comments','HTML::Parse');
    foreach (@module_list) {
      eval { require "$_" };
      if (!($@)) {
        print "Module Not installed : $_\n";
      }
    }
ikegami
  • 367,544
  • 15
  • 269
  • 518
made_in_india
  • 2,109
  • 5
  • 40
  • 63

2 Answers2

5

You need to use the string form of eval because require needs a bareword argument to match against the double-colon-separated form of the module name (e.g. Scalar::Util). (If it's not a bareword, then it needs to be a relative path, e.g. 'Scalar/Util.pm')

#!/usr/bin/perl

use strict;
use warnings;

my @module_list = ('Scalar::Util', 'flibble');

foreach (@module_list) {
    if (!eval "require $_") {
        print "Module not installed: $_\n";
    }
}
zgpmax
  • 2,777
  • 15
  • 22
  • 2
    `require` does accept an expression that's not a bareword, but it must evaluate to a file name. (e.g. `Smart/Comments.pm` instead of `Smart::Comments`) – ikegami Feb 09 '12 at 11:10
1

There's my App::Module::Lister, which I designed as a modulino that can run as a module, a command-line utility, or a CGI script. It's a simple thing I needed for a friend who only had FTP access to a web server.

It gives you the list of everything in Perl's module search path, which I usually find easier than checking for specific modules each time. Once I have the whole list, I just look at the list.

You could check that you can load the module, but I tend to not like that because I don't want to potentially run the module's code to see if it's installed. It's usually not a problem, though.

brian d foy
  • 129,424
  • 31
  • 207
  • 592