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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.