Gossamer Forum
Home : General : Perl Programming :

File Locking?

Quote Reply
File Locking?
  My script writes a txt file after running. The script is as follow.
---------------------------------------------
open(LOG,"today.txt");
flock(LOG, 2);
@lines = <LOG>;
close(LOG);
open(DATA,">yesterday.txt");
flock(DATA, 2);
print DATA @lines;
close(DATA);
unlink("today.txt");
---------------------------------------------
sometimes, yesterday.txt turn to a clear txt file, although today.txt contains some words. All words are deleted in this process. So... How can I modify the code to prevent yesterday.txt from losing information??
Thanks.
Quote Reply
Re: File Locking? In reply to
Well, error checking would be my first suggestion. Make sure to check the return value of all system calls: open, flock, unlink and even close if you are worried.

Cheers,

Alex
Quote Reply
Re: File Locking? In reply to
open(LOG,"today.txt") or die "Can't open file. Reason: $!";

etc.

What it will do is terminate the program upon an unsucessful attempt, and report back to you what the reason was.

--MArk

------------------
You can reach my by ICQ at UID# 8602162

Quote Reply
Re: File Locking? In reply to
how to error check? What's the advantages?