#! perl use strict; use warnings; use Fcntl ':seek'; my $file = 'aaa.txt'; if (!-e $file || -z $file) { # print "File '$file' is empty\n"; open(my $fh1, '>', $file) or die "Cannot open file '$file' for writing: $!"; print $fh1 "The quick brown fox jumped over the unfortunate dog.\n"; close $fh1 or die "Cannot close file '$file': $!"; } open(my $fh2, '+>>', $file) or die "Cannot open file '$file' for appending and reading: $!"; print $fh2 "We have nothing to fear but fear itself.\n"; seek $fh2, 0, SEEK_SET; print while <$fh2>; close $fh2 or die "Cannot close file '$file': $!";