in reply to Passing params, as a hash, to moose
So, if I'm understanding your problem correctly, you are building your object with undefined attributes and even if you have defaults set for the attributes, they still get set to undef, right?
I deal with this by using MooseX::UndefTolerant, whose brief description is: "Make your attribute(s) tolerant to undef initialization". One more thing: for this to work, it seems that you need to make your attribute lazy. Below is an example.
test_undef.pm:
package test_undef; use Moose; use MooseX::UndefTolerant; has 'language' => ( is => 'rw', isa => 'Str', default => 'Perl', lazy => 1, ); no Moose; __PACKAGE__->meta->make_immutable;
test_undef.pl:
#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use test_undef; use Getopt::Long; my $language; GetOptions( "language=s" => \$language, ); my $test = test_undef->new( 'language' => $language, ); say $test->language;
If I call ./test_undef.pl without the --language option (or w/o defining language any other way), the output is: Perl (the default I set for the language attribute).
|
|---|