Open it once with '+>' and then close it when your done writing any data to it. This will create the file, and then append to it until it is closed. If it is reopened, it will create a new file (deleting any existing data) and append to it until closed.
use strict;
use warnings;
open (my $file, '+>', 'file.txt') or die "cannot open or append: $!";
print($file "test write on line number: ", sprintf("%2d", $_)) && prin
+t ($file "\n") foreach (0..20);