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

Hi monks,
I once had this beautiful module, that was installed in the usual places (/usr/lib/perl5/site_perl/etc...).
I had inside a directory with datafiles used by my code, and was able to refer to it by $Config{sitelib}

Now I displaced the install directory under my local home:
/home/x/lib/perl/lib/perl5/site_perl/etc... and refer to it thanks to
export PERL5LIB=$PERL5LIB:/home/x/lib/perl/lib/perl5/site_perl/


BUT I noticed that $Config{sitelib} is not able anymore to inform me about this directory!
How do I find (simply if possible) where a running module is running from?
thanks monks!
alessandro

Replies are listed 'Best First'.
Re: finding installed module directory
by shmem (Chancellor) on Jul 07, 2006 at 14:44 UTC
    See perlvar - there's the special %INC hash, where modules loaded via do, require or use get recorded. The hash is keyed by module filename, the value is the path to it. The statement
    warn "$_ => $INC{$_}\n" for sort keys %INC;
    delivers the entire hash, sorted by keys, to STDERR.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Funny, I have this in my 'debug' function:
      print "$_ => $INC{$_}\n" for sort keys %INC;
      which is a standard part of every job I do, that why I can be usre which modules, and from where they have been loaded. It also means that you can add directories to the search path, and you can see just what is happening, for example:
      #!/usr/bin/perl -w use strict; use FindBin; use lib "$FindBin::Bin/lib"; use InContact::Util; print "$_ => $INC{$_}\n" for sort keys %INC;
      In my actual debugging module I read a single HASH with the /lib pointed to, then one with it disabled, then I can 'substract' one fomr the otehr and find out what the dependencies for my own modules are.

      jdtoronto

        thanks,with:
        my $jroot=""; foreach my $a (keys %INC) {if ($INC{$a}=~/(.*)jinit.pm/){$jroot=$1}} die "I cannot find datadirectory\n" if ($jroot eq "");
        I got what I wanted.
        Still, I don't like to use the pattern "jinit.pm" (the module name itself) hardcoded inside, to find the current directory - but it's just a problem of elegance, nothing more...

        alessandro