Click here to get back home

current directory, practical problem

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
current directory, practical problem cartercc 06-17-2008
Posted by cartercc on June 17, 2008, 9:42 am
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"; 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.

Posted by Ben Morrow on June 17, 2008, 10:57 am
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.

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

Ben

--
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. ben@morrow.me.uk

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

Similar ThreadsPosted
current directory name in perl August 14, 2006, 11:53 am
Perl and current directory December 9, 2006, 4:41 am
Apache vs IIS current directory fro Perl script May 8, 2008, 3:09 am
Platypus - Current Working Directory - write files November 5, 2007, 7:06 pm
Re: Platypus - Current Working Directory - write files November 6, 2007, 7:25 am
Platypus - Current Working Directory - write file November 6, 2007, 1:19 pm
Re: Platypus - Current Working Directory - write file November 6, 2007, 1:29 pm
Re: Platypus - Current Working Directory - write file November 6, 2007, 2:04 pm
Re: Platypus - Current Working Directory - write file November 6, 2007, 2:30 pm
Re: Platypus - Current Working Directory - write file November 6, 2007, 7:22 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap