Why not:
my $filepath = 'foo.txt'; open my $FOOFILE, '>>', $filepath or die "Couldn't open $filepath: $!\ +n"; print $FOOFILE "First line of text.\n"; for (1 .. 5) { #do a bunch of stuff do_other_stuff ($FOOFILE); } print $FOOFILE "The end."; close $FOOFILE;
Unless there are multiple applications that may be updating the file and that are fighting for locks on it (in which case you are in a whole other world of pain) there is no reason to close the file. Opening and closing a file can be relatively expensive so avoiding that cost is generally a Good Thing™.
Note that you should avoid using global variables in subs ($FOOFILE for example) and generally you shouldn't call subs using the &sub syntax. Also, use the Perl for loop rather than the C for loop where you can.
In reply to Re: Keep file open for sub call?
by GrandFather
in thread Keep file open for sub call?
by zod
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |