I'd like to set delegated attributes in my object constructors (e.g. as
below)  but this doesn't seem to work. Am I missing something?

Merlyn


#!/usr/bin/perl

package SubClass;

use Moose;

has thing => ( is => 'rw', isa => 'Int' );

before thing => sub {
    shift;
    print "subthing: ", @_ ? 'set to ' . (shift // '<undef>') : '(read)',
"\n";
    return;
};


package Class;

use Moose;

has sub_object => ( is => 'rw', isa => 'SubClass', handles => ['thing'],
builder => '_build_sub_object' );
#has sub_object => ( is => 'rw', isa => 'SubClass', handles => ['thing'],
default => sub { _build_sub_object(); } );

sub _build_sub_object {
    my $self = shift;

    print "_build_sub_object\n";
    return SubClass->new;
}


package main;

use strict;
use warnings;

print "Start\n";
my $object = Class->new(thing => 1);
print "Built\n";

print "Thing 1 = ", ($object->thing // '<undef>'), "\n";

$object->thing(2);

print "Thing 2 = ", ($object->thing // '<undef>'), "\n";

-- 

-- 
Merlyn

Reply via email to