I mainly agree, tie is an excellent way to encapsulate data and complicated behaviors. The encapsulation is not as tight as you suggest, though. The tied call returns the underlying blessed object. No B&D OO in Perl!

Your example is perhaps a little too simple. The hardwired key in your setter and getter has to be rewritten for each class you want. That could be generalized by allowing the constructor to accept a list of keys and then generate the FETCH and STORE methods as per-instance CODE refs. The class FETCH() and STORE() would then just call the instance's stored method.

That begins to sound a lot like a pseudohash, but with a completely different implementation.

The alternative is to make FETCH() virtual by declaring but not defining it. The user could then subclass your Tie:: class and define it there, or else define it externally.

After Compline,
Zaxo


In reply to Re: tie for Perlish, encapsulated objects by Zaxo
in thread tie for Perlish, encapsulated objects by Roy Johnson

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.