jkeenan1 has asked for the wisdom of the Perl Monks concerning the following question:
The module in question is object-oriented and, inside its constructor, several FileHandle objects are created and assigned as data members to the object created by the constructor.
sub new { my $class = shift; my $self = {}; $self->{fhalpha} = new FileHandle(">alpha"); $self->{fhbeta} = new FileHandle(">beta"); $self->{fhgamma} = new FileHandle(">gamma"); ... bless $self, $class; return $self; }
Note that the FileHandle objects are opened within the scope of this subroutine but are not closed within its scope. Instead, the filehandles are used for print calls inside a second subroutine and closed inside a third subroutine called by the second.
Do any dangers lurk therein?
Personally, I don't often use FileHandle, IO::File or similar packages to open and close filehandles. I'm content to use Perl's built-in open and close functions and -- since I've begun to drink the PBP kool-aid -- I always use lexically-scoped filehandles. So I'm always very conscious of the scope in which I open and close filehandles and would never open a handle without closing it in the same scope.
But, while I think my own practice is good, I don't want to advise my colleague to change his approach unless there are clearly things wrong with it. Are there?
TIA.
|
|---|