Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to find path for current module?

by ganeshk (Monk)
on Jul 22, 2005 at 12:33 UTC ( [id://477198]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
I have a very basic question.
I have module Mod. I need to determine the path of
this module inside the module.

I tried something like this.
Mod.pm
======
package Mod; use File::Spec::Functions qw(splitpath); print( (splitpath(__FILE__))[1], "\n");

This worked in some cases, but when I ran this command
from the same directory as Mod.pm, it didn't work.

perl -e 'use Mod;'
So I thought this approach was wrong. Could anyone
please tell me how to do it? And what was wrong
with my way?

Thanks very much in advance.

Edit g0n: replaced b tags with code tags

Replies are listed 'Best First'.
Re: How to find path for current module?
by davidrw (Prior) on Jul 22, 2005 at 12:40 UTC
    You can use the %INC hash (note it will be a relative path if it's in the current directory):
    print $INC{ __FILE__ }; use Data::Dumper; print Dumper \%INC;
Re: How to find path for current module?
by japhy (Canon) on Jul 22, 2005 at 12:52 UTC
    From the commandline, just use perldoc -l ModuleName. That's a lowercase "L" there.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to find path for current module?
by blazar (Canon) on Jul 22, 2005 at 12:38 UTC
    FindBin may be your friend. I'm not sure how it will work in modules though. A rapid peek into the documentation revealed a caveat in this sense... HTH anyway!

    Update: Nope! Wrong advice at all effects...

Re: How to find path for current module?
by ganeshk (Monk) on Jul 22, 2005 at 12:54 UTC
    Hi All,
    Thank you very much for those replies.

    But I want to get the absolute path of the module
    in the module's code.

    Thanks again.
        Hi All,
        Thanks so much.
        rel2abs(__FILE__) did the trick.

        Thanks

      Look into module Cwd.

      the lowliest monk

Re: How to find path for current module?
by tlm (Prior) on Jul 22, 2005 at 12:41 UTC

    I've never tried this before, but my first guess would be to look for the value in $INC{ 'Mod' }.

    Update: Not quite. %INC is the right place to look, but one needs filenames not package names for keys. I.e. what davidrw wrote.

    the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://477198]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-19 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found