>>>>> "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.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to