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
But hopefully in a more useful way than this trivial example...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; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |