: I know I'll take a beating for the unchecked opens etc.... but I'll ask
: anyway. I'm trying (unsuccessfully) to push a new user into a text file,
: but I have screwed up the the textbook example somehow.... I've tried a
: while (<>) loop etc. to no avail.
: ---------------- SNIPPET --------------------
: open(READ,$filename);
: open(TEMP,">$tempfile");
The first thing to do is, yes, to check the open status in order to find
out whether the problem is really in your code or in your directory
structure, permissions, etc.
: flock (TEMP, $EXCLUSIVE);
:
How do you know for sure that that's where the problem is? Did you print
order to see what, if anything, got pushed?
I don't see anything terribly wrong there, though I ought to point out
that you should make sure that the content of $newuser is terminated with
a newline; if it isn't, multiple invocations of your script will keep
appending to the last line rather than appending additional lines.
: flock (TEMP, $UNLOCK);
Not a good idea to unlock a file before closing it; newer versions of
Perl have some built-in defensive code to eliminate some race conditions
that could arise there, but it's good practice to avoid letting the
situation arise.
: close(READ);
: close(TEMP);
Check the results of those closes, too. If, for some reason, you were
unable to open TEMP for writing, you would *not* want to delete the original
file and then try to replace it with the file that doesn't exist because you
couldn't create it, would you?
: unlink($filename);
: rename($tempfile,$filename);
And check these too.