1 package MooseX::LazyRequire;
4 # ABSTRACT: Required attributes which fail only when trying to use them
7 use aliased 'MooseX::LazyRequire::Meta::Attribute::Trait::LazyRequire';
8 use namespace::autoclean;
11 Moose::Exporter->setup_import_methods;
14 my ($class, %options) = @_;
15 return Moose::Util::MetaRole::apply_metaclass_roles(
16 for_class => $options{for_class},
17 attribute_metaclass_roles => [LazyRequire],
30 MooseX::LazyRequire - Required attributes which fail only when trying to use them
41 use MooseX::LazyRequire;
50 builder => '_build_bar',
53 sub _build_bar { shift->foo }
55 Foo->new(foo => 42); # succeeds, foo and bar will be 42
56 Foo->new(bar => 42); # succeeds, bar will be 42
57 Foo->new; # fails, neither foo nor bare were given
61 This module adds a C<lazy_required> option to Moose attribute declarations.
63 The reader methods for all attributes with that option will throw an exception
64 unless a value for the attributes was provided earlier by a constructor
65 parameter or through a writer method.
69 Apparently Moose roles don't have an attribute metaclass, so this module can't
70 easily apply its magic to attributes defined in roles. If you want to use
71 C<lazy_required> in role attributes, you'll have to apply the attribute trait
75 traits => ['LazyRequire'],
90 Florian Ragwitz <rafl@debian.org>
92 =head1 COPYRIGHT AND LICENSE
94 This software is copyright (c) 2009 by Florian Ragwitz.
96 This is free software; you can redistribute it and/or modify it under
97 the same terms as the Perl 5 programming language system itself.