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

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?

This question is related to perl command-line perl-module

The answer is


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).


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"`;
 }'

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.


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)

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

From the perlvar entry on %INC:

  • %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.


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


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)


Quick and dirty:

$ perl -MXML::Simple -e 1

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

Examples related to perl

The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer "End of script output before headers" error in Apache Perl - Multiple condition if statement without duplicating code? How to decrypt hash stored by bcrypt Split a string into array in Perl Turning multiple lines into one comma separated line String compare in Perl with "eq" vs "==" how to remove the first two columns in a file using shell (awk, sed, whatever) Find everything between two XML tags with RegEx Difference between \w and \b regular expression meta characters

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to perl-module

In Perl, what is the difference between a .pm (Perl module) and .pl (Perl script) file? How is Perl's @INC constructed? (aka What are all the ways of affecting where Perl modules are searched for?) How can I check if a Perl module is installed on my system from the command line?