Now:
has 'id' => (
is => 'rw',
isa => 'Str',
default => sub { "id" . int(rand(1000))+1 }
);
Works fine,
PKG->new(id => 'some');
PKG->new()
In the following scenario:
my $value = undef;
PKG->new(id => $value);
(of course) got an error:
Attribute (id) does not pass the type constraint because: Validation failed for 'Str' with value undef at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-thread-multi-2level/Moose/Exception.pm line 37
The question arises:
How to achieve a value change after it is set to undef (and only when it is equal to $ undef)? In this way,
has 'id' => (
is => 'rw',
isa => 'Str|Undef',
default => sub { "id" . int(rand(1000))+1 }
);
Now he accepts $undef, but I do not want $undef, but I want "id" . int(rand(1000))+1. How to change attribute value after ?
aftercalled only for accessories, not for designers. Maybe some weird coercionfrom Undefto Str- but just for that one attribute?
Ps: use is PKG->new( id => $value // int(rand(10000)) )not an acceptable solution. The module must accept $undefand must silently change it to a random number.