I've been asked to do a review of a Perl module which a colleague has extensively refactored. In general, it's very good work. But there are points where I'd like to be able to distinguish between "This is not a good way to do it" and "This is not the way I myself would do it, but it's otherwise okay."

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.


In reply to FileHandle objects as data members of another object by jkeenan1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.