On Sat, Mar 21, 2009 at 11:05, Randal L. Schwartz <mer...@stonehenge.com> wrote: >>>>>> "Gunnar" == Gunnar Hjalmarsson <nore...@gunnar.cc> writes: > > Gunnar> Martin Spinassi wrote: >>> Is there any way to open a file for input and output at the same time? > > Gunnar> Yes. Open it with the '+<' MODE. > > Gunnar> open my $fh, '+<', $file or die "Couldn't open $file: $!"; > Gunnar> my @keep; > Gunnar> while ( <$fh> ) { > Gunnar> next if /^--/; > Gunnar> push @keep, $_; > Gunnar> } > Gunnar> seek $fh, 0, 0; > Gunnar> truncate $fh, 0; > > And then your power fails at this moment, and you've lost the entire file. > > Much safer to use in-place editing. See $^I in perlvar. snip
Based on my admittedly ignorant reading of the code for Perl 5.8.9, it looks like in-place editing does not do an fsync after the rename[1]. If this is true, then there is up to a five second window[2] of opportunity for a similar thing to happen to in-place editing on an Ext4 filesystem[3]. There is also the ongoing danger of putting untrusted file names in @ARGV[4], so make sure file names do not contain "|" before putting them in @ARGV. 1. I have asked about this on p5p in the "So what else is new?" thread. 2. http://tinyurl.com/d9pcfg 3. or any other filesystem that follows POSIX strictly on this point 4. http://perlgeek.de/blog-en/perl-tips/magic-argv.writeback -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/