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

Hi Monks, I have the following class:
package GSM::Cell; use Moose; use MooseX::AttributeHelpers; use Data::Dumper; has 'BCCH' => (is => 'rw', lazy => 1, default => &set_bcch); has 'LAC' => (is => 'ro', required => 1); has 'CI' => (is => 'ro', required => 1); has 'NAME' => (is => 'rw', lazy => 1, default => &set_name); has 'ID' => (is => 'ro', lazy => 1, default => \&set_id); with qw(SQLConnection); sub set_id { my $self = shift; return join(',',$self->LAC,$self->CI); } sub set_bcch { my $self = shift; my $sth = $self->dbh->prepare('select BCCHFrequency from Cell wher +e CI = ? and LAC = ?); $sth->execute($self->CI,$self->LAC); return $sth->fetchrow_array; } sub set_name { my $self = shift; my $sth = $self->dbh->prepare('select UserLabel from Cell where CI + = ? and LAC = ?'); $sth->execute($self->CI,$self->LAC); return $sth->fetchrow_array; } no Mouse; __PACKAGE__->meta->make_immutable; 1; #so the 'require' or 'use' succeeds
The SQLConnection role provides a DB handle to a mysql database. When an object is instantiated (example my $cell = GSM::Cell->new('LAC' => 203, 'CI' => 20021)), i would like the other attributes (BCCH,NAME) to be initialized with values from the mysql database. This is currently the purpose of the default functions set_name and set_bcch. While this works, i was hoping to use moose meta-programming to arrive at a DRYer solution. See http://babyl.dyndns.org/techblog/2009/05/moose-attribute-meta-meddling.html for a neat idea. The idea is that the resulting attributes would look something like this:
has BCCH => ( metaclass => 'SQLInitilaize', is => 'ro', table => 'Cell', )
with package SQLInitialize extendeding Moose::Meta::Attribute and implementing the necessary logic. The problem is that i would need access to the instance data $self->LAC and $self->CI for the SQL lookup (as can be seen in the set_bcch and set_name methods.
My questions on this:
1) Is there some way to access $self in the scenario described above?
2) Is there perhaps a better/more elegant approach to achieve the above?
3) Currently i am providing the DB handle to objects that need it via the SQLConnection role. I am not sure this is the best solution.
Any comments or suggestions ??

Replies are listed 'Best First'.
Re: Moose, SQL and initialization via Moose::Meta::Attribute
by stvn (Monsignor) on Jun 30, 2009 at 17:25 UTC

    Let me first just say that you do not need to do any metaprogramming for this. Metaprogramming should only be reached for when there is just simply no other way, it is not a tool for everyday use. In this case you can solve your problem with simple built in Moose features like lazy, default, build or lazy_build as several other posters have stated already.

    1) Is there some way to access $self in the scenario described above?

    I would suggest you use lazy with some combination of default or builder. metaperl above pointed to the relevant Recipe for this. In both the default and builder you will get a reference to $self as your first argument.

    I would likely recommend builder here since it would then treat the initializer as a method and so your subclasses can then take advantage of it. If this is not the desired behavior then you can use default.

    2) Is there perhaps a better/more elegant approach to achieve the above?

    No, what you have going looks fine to me. I would recommend that you do not store your default code refs in the package since they will be interpreted as methods by Moose. If that is what you want, then I suspect you should change from default to builder.

    3) Currently i am providing the DB handle to objects that need it via the SQLConnection role. I am not sure this is the best solution.

    We do something very similar at work with a WithDBICSchema role that provides access to the DBIx::Class schema object. It is a perfectly fine approach and is of the more common role usage patterns out there.

    -stvn
      Thank you monks for the good advice !
Re: Moose, SQL and initialization via Moose::Meta::Attribute
by NetWallah (Canon) on Jun 30, 2009 at 13:44 UTC
    My Moose-fu is pretty low, so no answers to your questions -- but shouldn't that be
    no Moose;
    instead of "no Mouse;" ?

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Moose, SQL and initialization via Moose::Meta::Attribute
by metaperl (Curate) on Jun 30, 2009 at 13:55 UTC
    i would like the other attributes (BCCH,NAME) to be initialized with values from the mysql database. This is currently the purpose of the default functions set_name and set_bcch.
    Well, I cant comment on a meta-approach, but in order that those attributes be retrieved once and then cached, you should use lazy_build as shown here

    I like that SQLConnection role - sweet!

Re: Moose, SQL and initialization via Moose::Meta::Attribute
by derby (Abbot) on Jun 30, 2009 at 16:52 UTC

    I'm not sure how to do this via meta ... but isn't this kinda what the BUILD method should be used for?

    package GSM::Cell; use Moose; use MooseX::AttributeHelpers; use Data::Dumper; has 'BCCH' => (is => 'rw'); has 'LAC' => (is => 'ro', required => 1); has 'CI' => (is => 'ro', required => 1); has 'NAME' => (is => 'rw', lazy => 1, default => &set_name); has 'ID' => (is => 'ro', lazy => 1, default => \&set_id); with qw(SQLConnection); ... sub BUILD { my $self = shift; my $sth = $self->dbh->prepare('select BCCHFrequency from Cell where + CI = ? and LAC = ?); $sth->execute($self->CI,$self->LAC); $self->BCCH( $sth->fetchrow_array ); } ...
    -derby