On Thursday 25 October 2007 09:30, Joseph L. Casale wrote: > I need to make some edits on small (~30 lines) text files. From > reading perldoc and recent posts, am I correct in understanding that > the proper way is to read the file in through one handle and write it > out through another? If that is the case, I suppose I need to write > in some code then to write the file out to a temp file, then either > move it over top or delete the old file and rename the new file? > > What is the accepted method in this scenario?
Since the file is relatively small you could slurp the entire file into memory, modify it, and then write it back out to the same file. For example (UNTESTED): use warnings; use strict; use Fcntl qw/ :flock :seek /; my $file = 'something'; open my $fh, '+<', $file or die "Cannot open '$file' $!"; flock $fh, LOCK_EX or die "Cannot flock '$file' $!"; read $fh, my $data, -s $fh or die "Cannot read '$file' $!"; # modify $data seek $fh, 0, SEEK_SET or die "Cannot seek on '$file' $!"; truncate $fh, 0 or die "Cannot truncate '$file' $!"; print $fh $data or die "Cannot print to '$file' $!"; close $fh or die "Cannot close '$file' $!"; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/