All the docs suggest I'm doing it right (in fact, this is pretty much copied
straight *from* the docs) but the role never actually gets applied.  No
composition-time errors are reported.  It fails at runtime if I attempt to
access the attribute defined in the role.

### MyApp/Role.pm
package MyApp::Role;

use Moose::Role;

has 'foo' => (
    is  => 'rw',
    isa => 'Str',
);

1;

### MyApp/Moose.pm
package MyApp::Moose;

use Moose ();
use Moose::Exporter;

Moose::Exporter->setup_import_methods(
    with_meta        => [ 'has_rw' ],
    also             => 'Moose',
    base_class_roles => ['MyApp::Role'],
);

sub has_rw {
    my($meta,$name,%options) = @_;
    $meta->add_attribute(
        $name,
        is => 'rw',
        %options,
    );
}

1;

### MyApp/Object.pm
package MyApp::Object;

use MyApp::Moose;

has 'name' => (
    is  => 'ro',
    isa => 'Str',
);
has_rw 'size' => (
    isa => 'Int',
);

no MyApp::Moose;

1;

### test.pl
#!/usr/bin/env perl

use MyApp::Object;

my $obj = MyApp::Object->new(name => 'asdf');
$obj->size(123);
$obj->foo('qwerty');

print join("\n", $obj->name, $obj->size, $obj->foo);

$ ./test.pl
Can't locate object method "foo" via package "MyApp::Object" at ./test.plline 7.

-- 
Stephen Clouse <[email protected]>

Reply via email to