Is it possible to create an attribute that can only be set in the constructor in Moose? I’d like to do something like this:
my $foo = new Foo(file => 'foo.txt');
my $bar = new Foo(string => $str);
$foo->file('baz.txt'); # dies
I know I can create an attribute that can not be set in constructor, but the complementary case seems to be missing.
Isn't that just a read-only attribute? If I write
package Foo;
use Moose;
has 'file' => (is => 'ro', isa => 'Str');
has 'string' => (is => 'rw', isa => 'Str');
1;
then your code dies with
Cannot assign a value to a read-only accessor