CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
flock problem

 



airman
Deleted

Mar 14, 2001, 2:19 PM

Post #1 of 2 (439 views)
flock problem Can't Post

I am trying to open a file, flock it, read it and then write to it. I am trying to do both the read and write under one flock so that there is no possiblility of someone opening the file between the read and write.

The following works fine:

open(INFILE, "file.htm");
flock(INFILE, 1);
@lines=<INFILE>;
close(INFILE);

open(OUTFILE, "+>$file.htm");
flock(OUTFILE, 2);
print OUTFILE "stuff";
close(OUTFILE);

The following does NOT work

open(OUTFILE, ">$file.htm");
flock(OUTFILE, 2);
@lines=<OUTFILE>;
print OUTFILE "stuff";
close(OUTFILE);





japhy
Enthusiast

Mar 14, 2001, 6:40 PM

Post #2 of 2 (433 views)
Re: flock problem [In reply to] Can't Post

The problem is that you're not opening the file for read-write. If you were using -w, Perl would have told you this.


Code
open FILE, "+< $file" or die "can't open $file for r/w: $!"; 
@lines = <FILE>;
seek FILE, 0, 0;
print FILE change(@lines);
close FILE;

Please read perldoc -f open

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives