I am using strawberry perl Moose 2.0010
In the class:
package Cat;
use 5.010;
use strict;
use Moose;
has 'name', is => 'ro', isa => 'Str', default => 'Beauty';
#has 'age', is => 'ro';
has 'diet', is => 'rw', default => 'fish';
has 'birth_year', is => 'ro', isa=> 'Int',
default => 1997;
sub age
{
my $self = shift;
my $year = (localtime)[5] + 1900;
return $year - $self->birth_year();
}
In the application:
use 5.010;
use strict;
use Cat;
my $kitty = Cat->new();
say 'I have a kitten named ', $kitty->name(), ' eats ', $kitty->diet(),
'in age ', $kitty->age();
Output:
Use of uninitialized value in subtraction (-) at Cat.pm line 16. I have a kitten named eats in age 2011 Press any key to continue . . .
The default value not set.
Thanks.
Your other question today shows what the problem is after you showed the full source to Cat.pm
. You had a stray new
method defined which overrode the new
method which Moose
supplies. Remove that stray new
method, and it works just fine.