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

Monks,

How do I determine whether a particular module is installed or not? I want to use Text::LevenshteinXS if it is installed (and working properly), else I want to use Text::Levenshtein, if all those fail, I want to use my own library (require Levenshtein.lib). How do I do that?

Thank you in advance.

Bogus code tags removed by GrandFather and p tags added.

  • Comment on how to determine whether a module is installed or not?

Replies are listed 'Best First'.
Re: how to determine whether a module is installed or not?
by tilly (Archbishop) on Dec 28, 2010 at 05:46 UTC
    Something like this (untested and I am rusty):
    eval {require Text::LevenshteinXS}; if ($@) { eval {require Text::Levenshtein}; else { require "Levenshtein.lib"; } }
      Thank you very much.
Re: how to determine whether a module is installed or not?
by GrandFather (Saint) on Dec 28, 2010 at 06:01 UTC

    Something like:

    if (eval {require 'Text/LevenshteinXS.pm'}) { Text::LevenshteinXS::import('distance'); } elsif (eval {require 'Text/Levenshtein.pm'}) { Text::Levenshtein::import('distance'); } else { *distance = \&myDistanceSub; }
    True laziness is hard work

      I found this on Google. this perl oneliner will list all the installed modules on your system (Linux). then you can grep for whatever module your are looking for.

      perl -MFile::Find=find -MFile::Spec::Functions -Tlwe 'find { wanted => + sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'
        That is an extremely inefficient solution.
Re: how to determine whether a module is installed or not?
by planetscape (Chancellor) on Dec 28, 2010 at 09:40 UTC
      I really liked your list!
      This is ActiveState specific for their ppm utility, but for those who have ActiveState, it is handy:

      $prompt>ppm profile save [my_config_file] (with no file, ouput goes to stdout)
      $prompt>ppm profile restore my_config_file

      The save option makes an XML file with lines like this:
      <SOFTPKG NAME="Text-LevenshteinXS" VERSION="0.03"/>

      This file also contains the ppm repositories that I have configured - not everything is at the main ActiveState repository. So if I want to "clone" my installation on another computer or for some reason do a complete re-installation on my own computer, I use the "save" command to a file. Then uninstall Perl, wipe out my Perl directory, re-install ActiveState Perl and then run the "restore" option to get all the packages re-installed. I just walked a friend through this last week and he said that it worked fine.

      I think the OP wanted to know what is going on at run time on some other "unknown system", but if we are talking about utilities, I think this is a good one for AS users and I have recent experience that it does indeed "work as advertised".

        I think the OP wanted to know what is going on at run time on some other "unknown system"

        I think you are right, but I wasn't quite sure when I posted my reply, and thought it might prove useful to future searchers who come upon the thread. Thanks for your addition.++

        HTH,

        planetscape
Re: how to determine whether a module is installed or not?
by samarzone (Pilgrim) on Dec 28, 2010 at 08:30 UTC

    evaluating (as given in above solutions) is the correct solution at run time but if you want a list of installed modules for a quick reference in future, there is a perl script 'instmodsh' which is shipped with Perl. (At least I have it installed with perl-5.8.8-18.el5 :) ). In my system it is installed at path /usr/bin/instmodsh so I can run it directly as a shell command.

    --
    Regards
    - Samar
Re: how to determine whether a module is installed or not?
by k_manimuthu (Monk) on Dec 28, 2010 at 08:50 UTC

    A command line option

    perl -MModule::Name -e l

    If the "Module::Name" installed it doesn't show the error. Otherwise it shows the error.

Re: how to determine whether a module is installed or not?
by ajguitarmaniac (Sexton) on Dec 28, 2010 at 14:26 UTC

    Checking for similar posts before posting really paid off! I was just about to post the exact same question (but I was looking to find the availability of another module). Thank you monks!

Re: how to determine whether a module is installed or not?
by JavaFan (Canon) on Dec 28, 2010 at 16:36 UTC
    eval {require "Text::LevenshteinXS"; Text::LevenshteinXS->import; 1} o +r eval {require "Text::Levenshtein"; Text::Levenshtein->import; 1} or do {require "Levenshtein.lib"; Levenshtein->import};