in reply to appending to a file
Append is ">>" (two >'s next to each other).
In other words, instead of this:
open FH, ">filename" or die "Bleah!\n$!";
File level appending is opened like this:
open FH, ">>filename" or die "Bleah!\n$!";
Have a look at perlopentut for details.
However, your code looks like it's trying to slurp in the file first, and then write it back out from scratch, while adding the new stuff at the end. Ok, that's one strategy, but it's not exactly the same thing as doing a file append.
As for your code, a better solution (since you also seem to be doing some processing on the current file, if it exists) would be this:
Open the input file. Open a temporary output file. Read the input file line by line. Process one line of input. Write it out to the output file. When done, append your additional information to the output file. Close the input file. Close the output file. Then rename your temp file so that it replaces your input file. Do your writing in standard ">" mode, because you're writing a new temp file, not appending to an existing file.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: appending to a file
by Anonymous Monk on Feb 26, 2004 at 06:46 UTC | |
by davido (Cardinal) on Feb 26, 2004 at 10:17 UTC | |
by revdiablo (Prior) on Feb 26, 2004 at 19:26 UTC | |
by Anonymous Monk on Feb 26, 2004 at 20:24 UTC | |
by davido (Cardinal) on Feb 26, 2004 at 23:43 UTC | |
|