Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Full path to the current file?

by RoUS (Initiate)
on Jan 08, 2008 at 14:47 UTC ( #661096=perlquestion: print w/replies, xml ) Need Help??
RoUS has asked for the wisdom of the Perl Monks concerning the following question:

How can I determine the full absolute filesystem path to the current file? Specifically, I have a module that needs to know its path in order to access another file in the same location. __FILE__ is giving me a relative path, somtimes funky, and I'm not sure to what it thinks it's relative..

Replies are listed 'Best First'.
Re: Full path to the current file?
by davidrw (Prior) on Jan 08, 2008 at 14:59 UTC
      Hi, ++davidrw
      or even
      $ perl -MFile::Spec -le 'print File::Spec->rel2abs( __FILE__ ) ;' /home/svenXY/-e # for a one-liner

      File::Basename is not necessary here.
        OP said I have a module that needs to know its path in order to access another file in the same location. -- the second step of File::Basename is to fulfill that requirement (even though he didn't directly ask for it).

        Other shell (bash) solutions:
        # setup cd /var/tmp/ touch foo f='../../var/tmp/foo' # for just the directory: d=`(cd ${f%/*} && pwd)` #or: d=`dirname $f` d=`cd $d && pwd` # for the filename (rel2abs): d=`dirname $f` d=`cd $d && pwd` f=`basename $f` f=`/bin/ls $d/$f`

      This works; thanks! The 'funky' aspect to which I referred is that when invoked from a script, the module's __FILE__ is 'XML/'. When invoked from the command line:

      perl -e "use XML::Foo; $x = new XML::Foo;'

      its __FILE__ is reported as '/XML/' -- which is clearly bogus.

      It has been suggested that the latter behaviour is due to Red Hat's patches for Fedora Core 8.

      Whatever, you've given me the solution. I guess I'll just have to wait and see if it works in all cases, or if there are edge conditions where it goes blooey.


Re: Full path to the current file?
by cdarke (Prior) on Jan 08, 2008 at 20:30 UTC
    If you want the full path name of where a module was loaded from, use %INC. For example:
    use File::Basename; print "$INC{'File/'}\n";
Re: Full path to the current file?
by cLive ;-) (Prior) on Jan 08, 2008 at 16:11 UTC
    I have a feeling the proposed solutions above don't give you the path relative to the module, but to the script. Cwd is the simplest solution though:
    #!/usr/bin/perl use strict; use warnings; use Cwd qw(abs_path getcwd); print "This script is: ".abs_path($0)."\n"; print "Working dir is: ".getcwd()."\n";

    but that won't tell you the path from the actual module.

    For that, you'll need to hard code the path to the module (I think):

    my $abs_filepath = abs_path("$module_dir/$relative_pilepath");
    __FILENAME__ is your friend...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://661096]
Approved by Corion
[tye]: Corion, I used to have my "last here" updated when I chatted. I still visit regularly but I rarely chat. So I modified my cookie today.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-04-28 14:52 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (524 votes). Check out past polls.