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

I am writing a project in Moose, which I'm fairly new to. The project will store and analyze a large amount of data, which for convenience purposes is being stored in a huge hash. What I would like is to, sometime in the future, tie the hash to a Moose object controlling a database so as to optimize data accesses. So, basically I know I want something like this:
package App::Foo; use Moose; has 'db' => ( isa => 'App::Foo::DB' );
where App::Foo::DB is just a blessed hash with no methods, so that I can use it in expressions like:
$self->db->{a}{b}{c} = 'foo';
What I don't know, is if its possible to rewrite App::Foo::DB so as to define a type of hash that is tied to a Moose class, and if so, how one should go about it. I guess the real question is how to define a Moose class with the interface of a tied hash.

Replies are listed 'Best First'.
Re: Tied Hash Interface to Moose Object.
by tobyink (Canon) on Feb 21, 2013 at 20:18 UTC

    Tie::MooseObject may be of interest to you - I'm not a fan of some of its design decisions though; I've often considered writing a replacement for it.

    (In particular, if attribute "foo" has accessors get_foo and set_foo I don't want to have to do $hash{set_foo} = $x and $x = $hash{get_foo}. I expect to be able to do $hash{foo} = $x and $x = $hash{foo}.)

    Update: I've finally gotten around to it.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Yes, I had noticed Tie::MooseObject, but as it doesn't actually make a drop-in replacement for a hash, it doesn't do what I need. I suppose I might use it as a starting point to write my own if need be though.