current directory, practical problem

C

cartercc

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.

Neither
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
files.)

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

Ben Morrow

Quoth cartercc said:
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.

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

Ben
 
M

MSwanberg

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.

Neither
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
files.)

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.

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:

($scriptpath)=$0=~m[(.*/)[^/]*$];

Then, it's a simple matter to do an

open(OUT,">$scriptpath$filename");

and then write to it.

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

($scriptpath)=$0=~m[(.*\\)[^\\]*$];

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top