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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.