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

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..

Comment on Full path to the current file?
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.
      Regards,
      svenXY
        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/Foo.pm'. When invoked from the command line:

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

      its __FILE__ is reported as '/XML/Foo.pm' -- 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.

      Thanks!

Re: Full path to the current file?
by cLive ;-) (Parson) 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...
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/Basename.pm'}\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-04-19 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls