Baby Moose tries again

It a little re-factor days here in the Moose-Pen

So just a quick re-factoring post-ette now that I have the basic CRUD working.

This line of code

my $field = $self->get_element_by_name(sub {$_->name eq $key});

was bugging me a little. You really have to have to be a fairly experienced Perl programmer before this type of call is second nature, or you have to be a real fan of List::Util to have this sort of call in you bag of tricks. So I think I will tone the level of this one down a little.

This means going back into Database::Accessor yet again and this time I will change only Database::Accessor::Roles::Common by first doing this;

has elements => (
isa => 'ArrayRefofElements',
is => 'ro',
traits => ['Array'],
handles => { element_count => 'count',
-- get_element_by_name => 'first',
++ _get_element_by_name => 'first',
default => sub { [] },

and then adding a new sub into that role like this;

sub get_element_by_name {
my $self = shift;
my ($name) = @_;
my $found = $self->_get_element_by_name(sub {$_->name eq $name});
return $found;

so all I am doing here is wrapping that array trait call to first in another sub which I pass the name I am looking for into it. You might as why I set and return the $found rather than just make the call to the '_get_element_by_name'? The reason if for garbage collection, it has been my experience with Moose that this style of return is less prone to memory leaks though that really should be a topic for a later post.

Now all I now have to do is change the two calls to ' get_element_by_name' in Driver::DBI;

--        my $field = $self->get_element_by_name(sub {$_->name eq $key});
++        my $field = $self->get_element_by_name( $key);

and rerun my test and they all passed so that is the post for today;


Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations