current directory, practical problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I know that Perl has a notion of the current directory, and that this
notion can be changed.

I wrote a script a couple of years ago that generates about a dozen
files, deleting some and moving others to various directories. Since
that time, others have modified it in various ways, unfortunately
without following through the dependencies. Late last week, the script
stopped working, and I was invited to fix it. I was able to compare
the script that I released with the current script and make it work.
In the process, I was requested to generate an additional file, to be
placed in the script directory. The script now generates the file, but
places it into another directory.

open RESULTS, ">results.txt";  nor
open RESULTS, ">./results.txt";
work, and I don't want to hard code the directory path because the
script gets moved from machine to machine and the path differs. (I use
a config file which initializes the relative paths of where to put the

The script is now 15 pages long and quite frankly I don't want to go
through it line by line -- I simply don't have the time. Any pointers
on how to fix this? Other than hard coding the absolute path? (I just
did this, and it works for now, until it's run on another server.)

Thanks, CC.

Re: current directory, practical problem

Quoted text here. Click to load it

So you don't want the current working directory, you want the directory
the script is in. Use FindBin.


I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.                                         

Re: current directory, practical problem

Quoted text here. Click to load it

It seems to me that the special variable $0 (that's a zero, not an oh)
has the path and filename of the executing script.  You could munge
that with some regex to get the directory/folder where the script is.

Something like:


Then, it's a simple matter to do an


and then write to it.

Just be careful... in DOS/Windows, the backslash is used in the path:



Site Timeline