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

Hi monks

I need to find the absolute path of some use'd modules.

%INC holds what I need, but Camel doesn't state whether %INC keys are OS dependent.

Eg, if I need the location of LWP::Simple, can I simply write:

  my $file = $INC{"LWP/Simple.pm"};

Or does "LWP/Simple.pm" need to be converted to whatever form is used on a specific platform.

Thanks for your help.

Replies are listed 'Best First'.
Re: Are %INC keys OS dependent?
by belg4mit (Prior) on Jul 22, 2003 at 23:53 UTC
    perl -Mstrict -Mwarnings -e "print join(',', %INC)" __END__ Carp.pm,C:/usr/perl/lib/Carp.pm,warnings.pm, C:/usr/perl/lib/warnings.pm, Exporter.pm,C:/usr/perl/lib/Exporter.pm, strict.pm,C:/usr/perl/lib/strict.pm
    Yes they are apparently system dependent, not that it ought to matter since perl generates the right paths for the platform you're on. If, for some reason you're generating things for another platform you could look at File::Spec.

    PS> You're asking about the values, not the keys ;-)
    PPS> The -M are used to load some modules, otherwise %INC is empty.

    --
    I'm not belgian but I play one on TV.

      You're asking about the values, not the keys ;-)

      Actually, I think the original poster was asking about the keys and not the values.

      If your code does this ...

      use LWP::Simple;

      ... then once the Simple.pm file has been located via @INC the pathname will be stored in %INC as the value associated with the key 'LWP/Simple.pm'.

      It is my understanding that a package name is always converted to a key by replacing '::' with '/' and appending '.pm' regardless of the naming conventions of the platform where the code is running. The full pathname to the file (ie: the value in %INC) is in a platform dependent format.

      So the answer to the original question: No the keys in %INC are not OS dependent.

      I've wanted to do this to convince perl that a module was already loaded. In fact I'd gotten the impression that it was stored in unix-form regardless of the platform but I guess I'm wrong. Oops! But definately the keys.

Re: Are %INC keys OS dependent?
by bart (Canon) on Jul 23, 2003 at 10:17 UTC
    I can confirm that MacPerl on the (pre OS-X) Macintosh uses "LWP/Simple.pm" as format for the keys to %INC.

    A few more things have been patched in MacPerl, in order to increase portability, so even stuff like

    do "LWP/Simple.pm";
    would work (for specific definitions of "work"). It's the same situation with require, and use, as these have been built on top of do.

    The normal file path separator on the Mac is ":". By contrast to Windows, "/" normally would not work. I distinctly recall being involved in a discussion about all this on the MacPerl mailing list a few years ago, but I'm currently still unsuccessful in tracing it back in the archives.
    Update: Found it!

    I have no idea on the situation on that other even more special case, VMS.

Re: Are %INC keys OS dependent?
by PodMaster (Abbot) on Jul 23, 2003 at 04:34 UTC
    Yes. `perldoc -f require'
    require Foo::Bar; # a splendid bareword The require function will actually look for the ``Foo/Bar.pm'' file in + the directories specified in the @INC array.
    I vaguely recall this question being raised on one of the lists, i'll see if I can find it (in case you require more confirmation).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Are %INC keys OS dependent?
by bobn (Chaplain) on Jul 22, 2003 at 23:46 UTC

    You could find out yourself if you have access to a couple of different platforms:

    # UNTESTED print join("\n", keys(%INC), "\n");

    --Bob Niederman, http://bob-n.com

Re: Are %INC keys OS dependent?
by Anonymous Monk on Jul 23, 2003 at 20:53 UTC
    Hi everyone

    Thanks for the replies - some folks have got the wrong idea though - I am talking about %INC keys, not values. Values will, of course, be OS dependent.

    perldoc perlvar discusses this:

      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.
    
    And that really doesn't give enough information IMHO.

    From grantrn and bart's replies it certainly looks like it's OS independent, and that makes far more sense to me.

    I'm going to perlbug this though, because I don't think the documentation is clear enough.

    Thanks everyone for your replies.