How can I check if a Perl module is installed on my system from the command line?

PerlCommand LinePerl Module

Perl Problem Overview


I tried to check if XML::Simple is installed in my system or not.

perl -e 'while (<@INC>) { while (<$_/*.pm>) { print "$_\n"; } }'

The above one-liner was used for listing all modules installed in my system. However, it is not listing XML modules.

However, the following executes fine.

perl -e "use XML::Simple "

What might be the issue?

Perl Solutions


Solution 1 - Perl

You can check for a module's installation path by:

perldoc -l XML::Simple

The problem with your one-liner is that, it is not recursively traversing directories/sub-directories. Hence, you get only pragmatic module names as output.

Solution 2 - Perl

Quick and dirty:

$ perl -MXML::Simple -e 1

Solution 3 - Perl

$ perl -MXML::Simple -le 'print $INC{"XML/Simple.pm"}'

From the http://perldoc.perl.org/perlvar.html">perlvar</a> entry on http://perldoc.perl.org/perlvar.html#%INC">`%INC`</a>;:

> - %INC > > The hash %INC contains entries for each filename included via the do, require, or use operators. The key is the filename you specified (with module names converted to pathnames), and the value is the location of the file found. The require operator uses this hash to determine whether a particular file has already been included. > >If the file was loaded via a hook (e.g. a subroutine reference, see require for a description of these hooks), this hook is by default inserted into %INC in place of a filename. Note, however, that the hook may have set the %INC entry by itself to provide some more specific info.

Solution 4 - Perl

For example, to check if the DBI module is installed or not, use

perl -e 'use DBI;'

You will see error if not installed. (from http://www.linuxask.com)

Solution 5 - Perl

If you want to quickly check if a module is installed (at least on Unix systems, with Bash as shell), add this to your .bashrc file:

alias modver="perl -e\"eval qq{use \\\$ARGV[0];\\\\\\\$v=\\\\\\\$\\\${ARGV[0]}::VERSION;};\ print\\\$@?qq{No module found\\n}:\\\$v?qq{Version \\\$v\\n}:qq{Found.\\n};\"\$1"

Then you can:

=> modver XML::Simple
No module found

=> modver DBI
Version 1.607

Solution 6 - Perl

What you're doing there is not recursing into directories. It is only listing the modules in the root directory of the @INC directory.

The module XML::Simple will live in one of the @INC paths under XML/Simple.pm.

What he said above to find specific modules.

CPAN explains how to find all modules here, see How to find installed modules.

Solution 7 - Perl

> while (<@INC>)

This joins the paths in @INC together in a string, separated by spaces, then calls glob() on the string, which then iterates through the space-separated components (unless there are file-globbing meta-characters.)

This doesn't work so well if there are paths in @INC containing spaces, , [], {}, *, ?, or ~, and there seems to be no reason to avoid the safe alternative:

for (@INC)

Solution 8 - Perl

If you're running ActivePerl under Windows:

  • C:\>ppm query * to get a list of all installed modules

  • C:\>ppm query XML-Simple to check if XML::Simple is installed

Solution 9 - Perl

I believe your solution will only look in the root of each directory path contained in the @INC array. You need something recursive, like:

 perl -e 'foreach (@INC) {
    print `find $_ -type f -name "*.pm"`;
 }'

Solution 10 - Perl

Bravo for @user80168's solution (I'm still counting \'s !) but to avoid all the escaping involved with aliases and shells:

%~/ cat ~/bin/perlmod
perl -le'eval qq{require $ARGV[0]; } 
    ? print ( "Found $ARGV[0] Version: ", eval "$ARGV[0]->VERSION" ) 
    : print "Not installed" ' $1

works reasonably well.

Here might be the simplest and most "modern" approach, using Module::Runtime:

perl -MModule::Runtime=use_module -E '
     say "$ARGV[0] ", use_module($ARGV[0])->VERSION' DBI

This will give a useful error if the module is not installed.

Using -MModule::Runtime requires it to be installed (it is not a core module).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoeView Question on Stackoverflow
Solution 1 - PerlAlan Haggai AlaviView Answer on Stackoverflow
Solution 2 - PerlSinan ÜnürView Answer on Stackoverflow
Solution 3 - PerlGreg BaconView Answer on Stackoverflow
Solution 4 - PerlHamidjonView Answer on Stackoverflow
Solution 5 - Perluser80168View Answer on Stackoverflow
Solution 6 - PerlSalgarView Answer on Stackoverflow
Solution 7 - PerlysthView Answer on Stackoverflow
Solution 8 - PerlZaidView Answer on Stackoverflow
Solution 9 - PerlGL2014View Answer on Stackoverflow
Solution 10 - PerlG. CitoView Answer on Stackoverflow