Re: "handles" accessor not being honoured in constructor?

[email protected] ("Chris Prather")
Newsgroups perl.moose
Message-ID <[email protected]>
The problem you're having is that your trying to initalize an attribute on a
class that doesn't *carry* that attribute.

    package MyClass;
    use Moose;

    has 'result' => (
        is      => 'rw',
        isa     => 'MyClass::Result',
        handles => [qw( query match score normalised_score )],
        default => sub { MyClass::Result->new() },
    );


Here you setup MyClass, and tell it to delegate the *accessor* method
score() ... that doesn't magically import the *attribute* score into
MyClass. So the initalizer fails to do anything because there's no attribute
named "score".

What you want is either, an attribute in MyClass called 'score' that passes
that attribute to MyClass::Result during construction of MyClass::Result, a
BUILD method that will populate this value on the result object, or to
re-think how you're setting up your class/attribute hierarchy if you really
want to populate the attributes of Object B via the constructor for Object
A.

-Chris
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.