Re: 'around' method modifier does not seem to work

[email protected] (Buddy Burden) Sat, 08 Aug 2015 16:27:51 -0700
Newsgroups perl.moose
Message-ID <[email protected]>
Marcos,

> around 'sequence' => sub
> {
>      my $orig = shift;
>      my $self = shift;
>
>      if (  @_ == 1 )
>      {
> my $sequence = _get_parsed_sequence( shift );
>          return $self->$orig( $sequence );
>      }
>      else
>      {
>          return $self->{sequence};
>      }
> };
>
> I get it to work.  Not a very elegant solution, for I must access the
> attribute directly instead of using an accessor.

Well, you have the accessor right there, if you'd like to use it: it's 
called `$orig`:

     return @_ ? $self->$orig( _get_parsed_sequence(shift) ) : $self->$orig;

> One issue still remains though, if I pass the argument to the constructor:
>
> my $collection = DCSE::Collection->new
> (
>     id => 'test'
>   , sequence => $sequence
>   , helix => $dcse->helix
> );
>
> I believe that 'around' is not called, for I get the unparsed argument
> when I fetch the attribute.

One of the few useful things I learned from C++ that's applicable to 
Moose: initialization != assignment.  In C++ you'd be stuck with 
duplicating some code, but in Moose ...

> I will look into 'writers' and 'readers', maybe they will work better in
> this case?

Look into "triggers." ;->


		-- Buddy