blazar has asked for the wisdom of the Perl Monks concerning the following question:

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".

Replies are listed 'Best First'.
Re: Blessing subrefs
by dave_the_m (Monsignor) on May 20, 2005 at 14:04 UTC
    Seems like a very memory-expensive way of doing things. You're creating per object: a CV, a hash, three arrays, plus a number of scalars roughly equal to the number of lexicals and ops in your closure code.

    Dave.

      Indeed. Certainly this is a downside that doesn't make this technique particularly suitable for those circumstances in which you may have to create many such objects. Of course speaking of it I'm more interested in its potential advantages momentarily disregarding memory efficiency issues...
Re: Blessing subrefs
by Animator (Hermit) on May 20, 2005 at 13:26 UTC

    I haven't used something like that before, but I can see another way to accomplish the same thing. But I have no idea which one is the 'best'... (nor do I know if it works for the full 100%, I only did some basic testing)

    Create a package to which you can tie a hash, add the restrictions/additions to in the STORE/FETCH method.

    Now you could tie a hash in your new-function to that package, and return a blessed reference to the tied hash.

    Now you could access the attributes as $self->{'attr'}, and the addition(s) as $self->{'rand120'} (for example ofc).

    Have you considered using this/a similar technique?

    (Note, I know that this wasn't your question but I thought it could be relevant/useful so I posted it...)

      This is a very intriguing technique, about which I have already thought as well (although in practice I have only very limited experience with tie), but that is closer in philosophy -AFAICT- to reblessing an object. Isn't it?
Re: Blessing subrefs
by dragonchild (Archbishop) on May 20, 2005 at 13:18 UTC
    This is actually the first design I was aware of that provided for true encapsulation. Inside-Out objects came from generalizing this solution, AFAIK.

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
      Interesting! Especially because although documents about these topics abound, they seldom give a historical perspective, which may be not terribly relevant nor of general interest, but which may also provide an alternative point of view for those who like and appreciate such an approach.
Re: Blessing subrefs
by demerphq (Chancellor) on May 20, 2005 at 13:26 UTC

    Sure you can do this. And as of the release of DDS v1.11 youll be able to dump them for debugging too. :-)

    ---
    $world=~s/war/peace/g

      Which circumstance suggests me that it must not be that rare, as a technique, after all...
A reply falls below the community's threshold of quality. You may see it by logging in.