Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: opening new file in different directory

by VSarkiss (Monsignor)
on Oct 23, 2001 at 18:53 UTC ( [id://120800]=note: print w/replies, xml ) Need Help??


in reply to opening new file in different directory

Umm, maybe I'm not following you correctly here, but it looks like you have a bit of a mish-mosh. Relative paths (like those starting with "..") start from your current directory. To do what you're looking for, you need to change your directory first. Something like this should work:

chdir "/home/main/sub/oldfiles"; # or wherever open READ, "$file" # open READ ">$file" is a typo, no? or die "Couldn't open $file to read: $!\n"; # ... Do stuff, ready to write open WRITE1, "> ../newfiles/$file" or die "Couldn't open $file to write: $!\n"; # ..Whatever... open WRITE2, ">../../cgi-bin/$file" or die "Couldn't open $file to write: $!\n";
This works fine on an HP-UX box, using 5.005_02.

Did I answer the right question?

HTH

Replies are listed 'Best First'.
Re: Re: opening new file in different directory
by George_Sherston (Vicar) on Oct 23, 2001 at 19:12 UTC
    I think this isn't the answer because I think (which I shd have spelled out) the current directory is /home/main/cgi-bin. The reason I'm a bit equivocal is that I do not explicitly set it to be the current directory; it's jus the directory where my script resides. Is that the problem? Why?

    § George Sherston

      Two things: First, depending on the server configuration, the script may or may not be executing in the directory in which the file is installed. It's not where the script is installed that matters, but the current directory of the Perl process that's executing your script.

      Second, more importantly: if you depend on a particular path, don't assume you're starting there, chdir to that directory explicitly. It guarantees your paths and has negligible overhead. If you're worried about spelling out the directory name in your script, read it in from the environment or some such. But don't assume your script will always execute in the same place.

        So wd you recommend, say, having a function that "sandwiches" each file operation between a pair of chdirs? I do quite a lot of reading and writing, but I also pick up a lot of files (templates and so on). At the moment I keep all the auxiliary files in the same dir as the script and refer to them by name only, no explicit path.

        /me scratches head and wonders whether I shd use explicit file paths for *everything* I refer to from my script. Wd that be considered good practice?

        § George Sherston

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://120800]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found