Moose read-only Attribute Traits and how to set them?

Posted by Evan Carroll on Stack Overflow See other posts from Stack Overflow or by Evan Carroll
Published on 2010-06-17T19:03:25Z Indexed on 2010/06/17 19:53 UTC
Read the original article Hit count: 445

Filed under:
|

How do I set a Moose read only attribute trait?

package AttrTrait;
use Moose::Role;
has 'ext' => ( isa => 'Str', is => 'ro' );

package Class;
has 'foo' => ( isa => 'Str', is => 'ro', traits => [qw/AttrTrait/] );

package main;
my $c = Class->new( foo => 'ok' );
$c->meta->get_attribute('foo')->ext('die') # ro attr trait

What is the purpose of Read Only attribute traits if you can't set it in the constructor or in runtime? Is there something I'm missing in Moose::Meta::Attribute? Is there a way to set it using meta?

$c->meta->get_attr('ext')->set_value('foo') # doesn't work either (attribute trait provided not class provided method)

© Stack Overflow or respective owner

Related posts about perl

Related posts about moose