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 ??

In reply to Moose, SQL and initialization via Moose::Meta::Attribute by ZAmonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.