Question

I'm trying to create a container class in Perl called Gene, which will store objects created by another class 'Cis' (so for example Gene1 will store Cis1a Cis1b Cis1c, and Gene2 will store Cis2a Cis2b Cis2c). This is what I have so far:

package Gene;

use Moose;

has 'bindingsites'=>(
isa=>'ArrayRef[Cis]',
is=>'rw',
default=>sub{[]},


package Cis;
use Moose;

has 'gene'=>(isa=>'Gene', is=>'rw', weak_ref =>1);

I was looking at one of the Moose Recipes and was trying to use that, but I'm not entirely sure it is what I am looking for. Using that I had written something like in the Gene class:

 sub Build{
 my(&self,$params)=@_;
 foreach my $bindingsite(@{$self->bindingsites}){
   $gene->bindingsite($self)}}

but I'm not sure that is what I need to do, and how each Gene class will know which Cis objects to store.

Thanks in advance for any help

Was it helpful?

Solution

If you want the gene attribute to be automatically updated when a Cis object is added to a the bindingsites of a Gene class, then you can use an 'after' method modifier in the Gene class, eg.

after 'bindingsites' => sub {
    my $self = shift;
    my $bindingsites = $self->bindingsites;

    $_->gene($self) for @$bindingsites;
};

This sub will be run after a call to $gene->bindingsites(...) and will iterate over the Cis objects setting the gene attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top