http://qs321.pair.com?node_id=120797

George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:

Sorry if this is obvious, but it isn't to me alas:

I want to open a file in one directory, read the contents, mess with them, and write them into another file with the same name (call it $file) in a different directory. Relative to where my script is, both of these directories are down one and up two. Now, to read first file I do open (READ, "/home/main/sub/oldfiles/$file") or die "could not open /home/main/sub/oldfiles/$file $!"; That's fine, although it wd be nice if I didn't have to write out the whole file path. I'd like to be able to do open (READ, "../sub/oldfiles/$file") or die "could not open ../sub/oldfiles/$file $!"; But that doesn't work. Perhaps it's not meant to work. Anyhow, the real problem comes when I want to write to a new file:
open (READ, ">/home/main/sub/newfiles/$file") or die "could not open / +home/main/sub/newfiles/$file $!";
d-oesn't work. At the moment what I'm doing is
chdir "/home/main/sub/newfiles"; open (WRITE,">$file") or die "could not open $file $!"; # write stuff chdir "/home/main/cgi-bin";
But that seems clunky. I'd welcome any advice how to do it nicer, and any explanation of why the different things I'd like to do don't work. As you can see I don't really understand the science of directories very well: I long for enlightenment.

§ George Sherston