Say I have a class My::A; at the time of the development cycle, it's determined that it would a good idea that there be some class My::B which can only be created through My::A and manipulated by My::A; My::B objects are not passed outside of My::A code. So for all effective reasons, My::B can be considered a private class of My::A.

Now, in C++ or Java, you can use the combination of access restrictions on classes and/or internal classes such that My::B would have been sufficiently hidden from any external code save for My::A. Typically, the My::B class would be in the same file as My::A declarations and definitions.

In perl, it's nay impossible to hide such a class (TTBOMK). So there's two possible options I can see: either put the definition of My::B in My::A's file as:

# ./My/A.pm package My::B; # ... # ... package My::A; # ... # ...
or to put B into it's own file:
# ./My/A.pm package My::A; use My::B; # ... # ... # ./My/B.pm package My::B; # ... # ...
In neither case, My::B isn't protected as the user would still be able to gain access to My::B, so worrying about this issue is a forgone conclusion. Thus, it's now mostly a semantics question -- which is the better or preferred way of doing this? The only advantage that it would see to me is using separate files, it's more obvious during use statements that the user is bringing My::B into the block, as opposed to the single file approach; of course, this again makes My::B more accessible that it might seem.

(Now of course, I put the cavaet that such hiding may be determined by the initial design, but down the road, it may be determined that allowing the user to inherit from My::B may be useful or important, so the fact that such code is not hidden by perl to start may be a good thing.

Update I should state that I fully understand that I cannot 'protect' my class from being instaniated as demerphq points out (and I like his analogy as well). Given that, I'm more looking for how I place the class def into files as to indicate to the user as best as possible that they really shouldn't instaniate the class, and/or to make it fit the perl philosophy more.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


In reply to Private Classes - Same or Separate Files? by Masem

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.