|
Posted by MSwanberg on June 23, 2008, 3:43 pm
Please log in for more thread options > 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"; =A0nor
> 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)=3D$0=3D~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)=3D$0=3D~m[(.*\)[^\]*$];
-Mike
|