# Perl - Modules and path on disk

I don't know if you have ever wondered where a module that you have been using is installed. Working in a corporate with multiple operating systems and having to maintain data update systems on multiple platforms that run on Perl based scripts, you might have to make sure that the CPAN modules you use are of the same versions, to keep your code portable. So recently I ran across one of these problems and came up with some interesting solutions as suggested by my colleague. I didn't understand what I was doing at the time. So I took a break and tried to understand how I actually got to know what it was.

So in order to find out where a Perl module lives you have to run the following:

 1$perl -MModuleDir::ModuleName -e 'print$INC{"ModuleDir/ModuleName.pm"}'
2
3Wow! what sorcery was that? Cool isn't it? So what does all mean?
4In Perl
5@INC
6and
7%INC
8stores information about modules and their locations. This is where the interpreter knows where to look for a module. The array stores the different paths that Perl would look for, to find modules. The hash contains the module name and the file it was loaded from, which is where the module lives. Now you must be wondering but the module name should be something like
9ModuleDir::ModuleName
10and not
11ModuleDir/ModuleName.pm
12. Yea, you are right, your argument is perfectly sensible. But Perl doesn't store things in the hash like that. It does it as if it was a file specification. So good luck finding your module's path the other way :).
13So in order to explain more about what that magical line of code means, you have to know what the -M option does or means. So I'm borrowing something from the official Perl documentation [perlrun](http://perldoc.perl.org/perlrun.html):
14\-m$-$module
15\-M$-$module
16\-M$-$'module ...'
17\-$mM$$-$module=arg$,arg$...
18
19\-mmodule executes use module (); before executing your program.
20
21\-Mmodule executes use module ; before executing your program. You can use quotes to add extra code after the module name, e.g., '-MMODULE qw(foo bar)'.
22
23If the first character after the -M or -m is a dash (-) then the 'use' is replaced with 'no'.
24
25A little builtin syntactic sugar means you can also say -mMODULE=foo,bar or -MMODULE=foo,bar as a shortcut for '-MMODULE qw(foo bar)'. This avoids the need to use quotes when importing symbols. The actual code generated by -MMODULE=foo,bar is use module split(/,/,q{foo,bar}) . Note that the = form removes the distinction between -m and -M.
26
27A consequence of this is that -MMODULE=number never does a version check, unless MODULE::import() itself is set up to do a version check, which could happen for example if MODULE inherits from Exporter.
28
29That should be clear, because I really can't explain it any better. So I will leave it there. Going ahead to explore what \-e means when passed to perl. You could find more about it from the perl documentation itself. But this time I have a simpler way to say it, it basically tells perl that you are writing a one-liner and not asking perl to run a program from a script.
30
31I hope that helps.
`