use IO::File qw( &SEEK_SET ); my $fh = IO::File->new("+) { my $line = $_; chomp $line; $line =~ s/foo/f/g; push @output, $line; } my $output = join("\n", @output); $fh->seek( 0 , SEEK_SET ); $fh->print($output, "\n"); $fh->truncate( length($output) ); $fh->close; exit 0; #### use Fcntl qw( &SEEK_SET ); open(FH, "+) { my $line = $_; chomp $line; $line =~ s/foo/f/g; push @output, $line; } my $output = join("\n", @output); seek(FH, 0 , SEEK_SET ); print FH $output, "\n"; truncate(FH, length($output) ); close(FH); exit 0;