develooper Front page | perl.moose | Postings from August 2015

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

Thread Previous | Thread Next
From:
Buddy Burden
Date:
August 8, 2015 23:28
Subject:
Re: 'around' method modifier does not seem to work
Message ID:
55C69077.3040703@gmail.com
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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About