Dear monks,

I'm doing perl OO for $work. Under normal circumstances I'd use a modern framework (Object::InsideOut, Moose, ...), but for various (politic) reasons this isn't possible here. So, I have to stick to whatever can be found in the perl 5.8.8 core modules and use classic blessed hashes for the implementation.

As my hierarchy of classes is expected to grow huge over the time, I'd like to protect their internals from each other. For example, my base class has a 'session' data member which is expected to be private as it is not part of the documented interface. This is all fine and nifty, but there is nothing guaranteeing against a sub-sub-sub-class trying to stuff its own 'session' in the same blessed hash and introducing a subtle bug - and a world of pain.

So far I've guarded against this using a __PACKAGE__ prefix, but I've recently stumbled upon what seems to be a hidden treasure: the fields pragma. It has the following features:

  1. Package-private hash members (_ prefix),
  2. Compile-time (!) check against typoes in the hash access (if declared using my Type $var),
  3. For perl 5.8.x, more efficient access and storage than regular hashes (as it is implemented as a pseudo-hash and the lookup is done at compile time).

Of these, I'm primarily interested by 1, although 2 is pretty nice as well, and 3 is always a bonus. However, is there any known pitfalls with this pragma? I've found this resource about it only, and its main criticism was that fields are "bizarre". Anyone has got some real-world experience with it?


In reply to Fields pragma - the poor man's OO framework? by waba

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.