Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday November 23, 2009
06:40 AM

Hash Objects and AutoInflation

[ #39931 ]

Until the issues are sorted out with, I'll still be posting here for a while (probably cross-posting for a while after, too).

Recently I've wanted a simple hash object:

my $object = Hash::Object->new({
  id   => $some_id,
  this => 'that',
  name => 'john',

print $object->name; # john

The problem is that current implementations I've found have limitations. One uses AUTOLOAD so any method name becomes valid. Another doesn't do that, but it also does nothing else. I'd like to be able to autoinflate an object such that if I call an unknown method, it inflates the hash object into a full-blown object and redispatches to that object, only displaying a "Can't locate object method" error if the "inflated" version of that object doesn't provide the requested method.

my $object = Hash::Object->new(
  data => {
    id   => $some_id,
    this => 'that',
    name => 'john',
  inflate => sub { Customer->find({ id => shift->id }) }

print $object->name;

# inflates to full Customer object and redispatches method
print $object->fullname;

The reason for this is that we sometimes have very expensive objects, but we only need one or two pieces of data from them. It would be nice to return a "proxy" object, containing the data we probably want, but will transparently work like the real thing if needed.

This is on the CPAN, yes? What's it called?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Sorry to hijack your thread but I have just tried to register unsuccessfully for the last 10 minutes. It keeps telling me the captcha text I am entering is wrong despite being 100% confident that I have it correct.

    I am ready to abandon my journal here but can't seem to get registered there. About the only thing I can see that might be an issue is that I am forced to use IE and the register page says "finished but with errors".

    • Right now it looks like it needs a lot more RAM. Dave Cross and SixApart are working on that side of things. We'll have it working, though. It's just a matter of being patient. And you're not the only person to tell me you're ready to abandon use.perl, so be patient and you'll be able to do so.

  • I asked a similar question [] a while back. The answers there may have what you're looking for.

  • I got very interested a while back, and stumbled on a slew of distributions that do similar things. In addition to those that are mentioned in reply to revdiablo's post, would any of Object::Lazy [], Class::Delay [], or Object::Trampoline [] (obtained by searching for 'delay' on CPAN) do something like what you'd like?