Re: Default values for undef attributes

[email protected] (WOLfgang Schricker)
Newsgroups perl.moose
Message-ID <[email protected]>
Octavian Rasnita schrieb:
> Hi,
[...]
> package TestMod;
> 
> use Moose;
> 
> has foo => (is => 'ro', isa => 'Int', default => 123);
> 
> __PACKAGE__->meta->make_immutable;
> 
> package main;
> 
> use strict;
[...]
> #This doesn't work because foo is undefined:
> my $t = TestMod->new(foo => undef);
> print $t->foo;
> 
package TestMod;

use Moose;

has foo => (is => 'ro', isa => 'Int|Undef', default => 123);

__PACKAGE__->meta->make_immutable;

package main;

use 5.012;
use Data::Dumper;

my $test_0 = TestMod->new();
my $test_1 = TestMod->new(foo => 456);
my $test_2 = TestMod->new(foo => undef);

say Dumper($test_0, $test_1, $test_2);

See 'isa' ;-)
-- 
Regards
*WOL* fgang *S* chricker
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.