in reply to Keep file open for sub call?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Keep file open for sub call?
by zod (Scribe) on Nov 20, 2008 at 03:50 UTC |