Initially topic was started here, but I need a working code example how to properly delegate attributes with Moo or Moose.
Based on documentation I wrote this code to check:
package Cat;
use Moo;
has 'token' => ( is => 'rw', default => '12345' );
has 'tiger' => ( is => 'rw', default => sub { my $self = shift; Cat::Tiger->new(token => $self->token) }, handles => [ qw(token) ] );
package Cat::Tiger;
use Moo;
extends 'Cat';
# + some additional methods
package main;
use Data::Dumper;
my $cat = Cat->new(token=>'54321');
warn $cat->token;
warn $cat->tiger->token;
But this code produce an error:
You cannot overwrite a locally defined method (token) with a delegation at 3.pl line 5
If I remove handles => [ qw(token) ]
at line 5 code will return another error:
Deep recursion on subroutine "Tiger::new" at 3.pl line 5.
So how to do?
I need to set token of Cat::Tiger
object ($cat->tiger->token
) same as in Cat
object ($cat-token
) and synс them everytime when token of Cat
object changed.