in reply to Keep file open for sub call?

Assuming the amount of data you are writing to a file is relatively small, another approach is to accumulate the lines of data into an array, then open/write/close the file in one place:
my @lines; push @lines, "First line of text.\n"; # keep pushing lines of text into the array my $filepath = 'foo.txt'; open my $FOOFILE, '>>', $filepath or die "Couldn't open $filepath: $!" +; print $FOOFILE $_ for @lines; close $FOOFILE;

You could pass the array as a reference to your sub.