zod has asked for the wisdom of the Perl Monks concerning the following question:
So I thought I ought to do it like this:
My question is whether this is poor form. Should I close and reopen the file in the sub instead of just leaving it open? More like this:my $filepath = 'foo.txt'; open my $FOOFILE, '>>', $filepath or die "Couldn't open $filepath \n"; print $FOOFILE "First line of text.\n"; for ( my $outer_loop = 1 ; $outer_loop <= 5 ; $outer_loop++ ) { #do a bunch of stuff &do_other_stuff; if ( $outer_loop == 5 ) { print $FOOFILE "The end."; close $FOOFILE; } } sub do_other_stuff { for ( my $inner_loop = 0 ; $inner_loop < 5 ; $inner_loop++ ) { if ( $inner_loop == 3 ) { print $FOOFILE "A match!\n"; } }
Thanks,sub do_other_stuff { open $FOOFILE, '>>', $filepath or die "Couldn't open $filepath \n"; for ( my $inner_loop = 0 ; $inner_loop < 5 ; $inner_loop++ ) { if ( $inner_loop == 3 ) { print $FOOFILE "A match!\n"; } } close $FOOFILE; }
zod
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Keep file open for sub call?
by GrandFather (Saint) on Nov 20, 2008 at 03:12 UTC | |
by zod (Scribe) on Nov 20, 2008 at 03:50 UTC | |
|
Re: Keep file open for sub call?
by lostjimmy (Chaplain) on Nov 20, 2008 at 03:15 UTC | |
|
Re: Keep file open for sub call?
by toolic (Bishop) on Nov 20, 2008 at 14:55 UTC |