I just started learning Moose, and I've created a very basic class. Here is my code:
Person.pm
package Person;
use Moose;
has fname => (
is => 'rw',
isa => 'Str',
reader => 'getFirstName',
);
has lname => (
is => 'rw',
isa => 'Str',
reader => 'getLastName',
writer => 'setLastName',
);
sub printName {
my $self = shift;
print $self->getFirstName() . " " . $self->getLastName(), "\n";
}
no Moose;
__PACKAGE__->meta->make_immutable;
person.pl
#!/usr/bin/env perl
use strict;
use warnings;
use Person;
my $person = Person->new(fname => 'jef', lname => 'blah',);
print $person->fname, $person->lname, "\n";
$person->setLastName('bleh');
$person->getName();
Where this code dies is line 8. It will print out the first name attribute, but it will whine about lname Can't locate object method "lname" via package "Person" at ./person.pl line 8.
Now, if I take out the writer
in lname, all is fine, but how does that make sense? I realize I can use the getters that I created, but I'm curious why a writer would then deny me access to the attribute itself? I guess I'm not understanding something...