One more question on the way to OOnlightenment...

I have recently read May Thy Closures Be Blessed, and I must say that (taking into account that a hash is fundamentally a function on a finite set, with the exception that in some sense its domain is unlimited, nay, too unlimited for many people's taste) I would have thought of something along the same lines if I wanted to put some restrictions on the attributes, rather than following the alternative path of Inside-Out objects, which spots strict checking of lexical variables instead.

Now the question is about this: one possibility that strikes me as interesting is that of a blessed closure that alternatively to restricting access to attributes or in addition to restricting them (in some sense) could extend it (in some other sense) a' la

sub new { my $class=shift; my %attribs = ( # watever ); bless sub { my $field=shift; return rand $1 if $field =~ /^rand(\d+)$/i; # normal retrieval of attributes from %attribs here }, $class; }
But hopefully in a more useful way than this trivial example...

And the question is: has anybody done anything similar? Not just for experimenting, but for practical use, that is.

Note: I am aware that one may move such functionality to an accessor method instead. But the more I learn about OO in Perl the more I think that this could be said of practically anything, due to the nature of its object model. Still I'm now specifically interested in an implementation at the level of the "actual attributes storage".


In reply to Blessing subrefs by blazar

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.