use IO::File; my $fh = IO::File->new( ">>$filename" ) or die "Cannot open '$filename' for appending: $!\n"; # Do your stuff here. At the end of every iteration, call $fh->flush; $fh->close;