views:

1601

answers:

7

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?

+14  A: 

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.

Alan Haggai Alavi
Heh. Beat me by a couple of seconds. Deleted my identical answer :)
Nic Gibson
@newt: Haha. :-)
Alan Haggai Alavi
arg 2 minutes late
Salgar
Of course, this will only work if the module you are looking for contains POD.
innaM
@Manni: Should work with most modules (from CPAN) as all of them are documented.
Alan Haggai Alavi
Some of them are so well documented that they have their docs in their own .pod files. It's a nice trick that works most of the time with that one little gotcha.
innaM
This only works if the module contains Pod. Strange but true.
brian d foy
+7  A: 

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. How to find installed modules

Salgar
+1 for explaining the problem with the OP's code.
Sinan Ünür
+10  A: 
$ 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.

Greg Bacon
+2  A: 

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)
ysth
+2  A: 

If you want to quickly check if 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
depesz
+3  A: 

Quick and dirty:

$ perl -MXML::Simple -e 1
Sinan Ünür
Although, if you want the version, most people say "perl -MModule\ 999".
jrockway
A: 

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

Zaid