As a simple answer that at least makes the get/set easier, you can write your own accessor functions to wrap an array ref into an array. For example:
package Person;
use Moo;
has 'friendsRef' => ( is => 'rw', default => sub { []; } );
## A 'set/get' wrapper for friendsRef
sub Person::friends {
my ($self, @list) = @_;
return @{$self->friendsRef} unless $#list>=0;
return $self->friendsRef(\@list);
}
## Example bonus function for adding to list
sub Person::addFriends {
my ($self, @list) = @_;
return push(@{$self->friendsRef}, @list);
}
Example code usage could be:
my $p = Person->new();
$p->friends('Dave','Jerry');
print "Friends are: ",join(', ',$p->friends()),"\n";
$p->friends('Bob','Dave');
print "Friends are: ",join(', ',$p->friends()),"\n";
$p->addFriends('Joe','Cletus');
print "Friends are: ",join(', ',$p->friends()),"\n";
## Note that "$p->friends()" is not a scalar, it's a sub returning an array,
## so instead of "$#{$p->friends()}" you'd want "scalar($p->friends())"
print "Number of friends is ",scalar($p->friends()),"\n";
Output is:
Friends are: Dave, Jerry
Friends are: Bob, Dave
Friends are: Bob, Dave, Joe, Cletus
Number of friends is 4
default => ()makes no sense. It's just a weird way of writing'default'.