8

I have a perl script which is using relative file paths.

The relative paths seem to be relative to the location that the script is executed from rather than the location of the perl script. How do I make my relative paths relative to the location of the script?

For instance I have a directory structure

dataFileToRead.txt
->bin
  myPerlScript.pl
->output

inside the perl script I open dataFileToRead.txt using the code my $rawDataName = "../dataFileToRead.txt"; open INPUT, "<", $rawDataName;

If I run the perl script from the bin directory then it works fine

If I run it from the parent directory then it can't open the data file.

Dunc
  • 7,688
  • 10
  • 31
  • 38

4 Answers4

14

FindBin is the classic solution to your problem. If you write

use FindBin;

then the scalar $FindBin::Bin is the absolute path to the location of your Perl script. You can chdir there before you open the data file, or just use it in the path to the file you want to open

my $rawDataName = "$FindBin::Bin/../dataFileToRead.txt";
open my $in, "<", $rawDataName;

(By the way, it is always better to use lexical file handles on anything but a very old perl.)

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • @daxim: your comment is presumably the mantra that *FindBin is broken*. I know about this, but cannot easily find an explanation, an example of its failure relevant to this question, or an alternative module. Perhaps you can help us? – Borodin Mar 01 '12 at 14:06
  • Direct links: [FindBin is broken](http://www.perlmonks.org/?node_id=41213) (fixed in core since mid-2011 only); [FindBin seems to think it's necessary to walk the whole path](http://use.perl.org/comments.pl?sid=36400&cid=56760); [code alternative](http://use.perl.org/~Aristotle/journal/33996) or perhaps FindBin::Real or lib::abs. I never had the desire to do this comparison because I never needed FindBin or functional equivalent because I know about packaging and the share dir feature. – daxim Mar 01 '12 at 15:14
3

To turn a relative path into an absolute one you can use Cwd :

use Cwd qw(realpath);
print "'$0' is '", realpath($0), "'\n";
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
JRFerguson
  • 7,426
  • 2
  • 32
  • 36
  • Documentation of Cwd is currently available here: http://search.cpan.org/~smueller/PathTools-3.47/Cwd.pm – Jean-Francois T. May 11 '15 at 02:52
  • 2
    I've updated the path to the Cwd docs to a permanent path: http://search.cpan.org/dist/PathTools/Cwd.pm That path will always be the latest, even if the version changes or even the owner. – Andy Lester Feb 08 '17 at 21:22
1

Start by finding out where the script is.

Then get the directory it is in. You can use Path::Class::File's dir() method for this.

Finally you can use chdir to change the current working directory to the directory you just identified.

So, in theory:

chdir(Path::Class::File->new(abs_path($0))->dir());
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Relative paths are relative to the current working directory. If you don't have any control over the working directory then you need to find a more robust way to spcecify your file paths, e.g. use absolute paths, or perhaps relative paths which are relative to some specific location within the file system.

Paul R
  • 208,748
  • 37
  • 389
  • 560