I'm studying Moo and bumped into this basic question. If I set an accessor or a writer name for a read-only attribute the attribute becomes writable. Do accessors or writers imply that the attribute is writable even if it's set to read-only (is => 'ro')?
Here is the Class code:
#!/home/fl/perl5/perlbrew/perls/perl-5.26.1/bin/perl
package MooPerson;
use Moo;
use namespace::clean;
has firstname => (
is => 'rw',
reader => 'get_firstname',
writer => 'set_firstname',
);
has surname => (
is => 'ro',
reader => 'get_surname',
writer => 'set_surname',
);
sub get_fullname {
my ($self) = @_;
my $firstname = $self->get_firstname;
my $surname = $self->get_surname;
return "$firstname $surname";
}
1;
Object code:
#!/home/fl/perl5/perlbrew/perls/perl-5.26.1/bin/perl
use lib 'lib';
use MooPerson;
use feature 'say';
use strict;
use warnings;
say "new object person";
my $person = MooPerson->new(
firstname => 'Homer',
surname => 'Simpson',
);
say "person->get_firstname: " . $person->get_firstname();
say "person->get_surname: " . $person->get_surname();
say "\nchange firstname and surname";
$person->set_firstname('Aristotle');
$person->set_surname('Amadopolis');
say "person->get_firstname: " . $person->get_firstname();
say "person->get_surname: " . $person->get_surname();
Result:
fl@dancertest:~/perltest$ ./firstMoo.pl
new object person
person->get_firstname: Homer
person->get_surname: Simpson
change firstname and surname
person->get_firstname: Aristotle
person->get_surname: Amadopolis
The same behavior occurs when I use accessor. (is => 'ro') only works if I use the auto generated accessor name, in this case "surname".
Is it an intended behavior or a bug?
Thank you very much.