1MooseX::LazyRequire(3)User Contributed Perl DocumentationMooseX::LazyRequire(3)
2
3
4
6 MooseX::LazyRequire - Required attributes which fail only when trying
7 to use them
8
10 package Foo;
11
12 use Moose;
13 use MooseX::LazyRequire;
14
15 has foo => (
16 is => 'ro',
17 lazy_required => 1,
18 );
19
20 has bar => (
21 is => 'ro',
22 builder => '_build_bar',
23 );
24
25 sub _build_bar { shift->foo }
26
27
28 Foo->new(foo => 42); # succeeds, foo and bar will be 42
29 Foo->new(bar => 42); # succeeds, bar will be 42
30 Foo->new; # fails, neither foo nor bare were given
31
33 This module adds a "lazy_required" option to Moose attribute
34 declarations.
35
36 The reader methods for all attributes with that option will throw an
37 exception unless a value for the attributes was provided earlier by a
38 constructor parameter or through a writer method.
39
41 Apparently Moose roles don't have an attribute metaclass, so this
42 module can't easily apply its magic to attributes defined in roles. If
43 you want to use "lazy_required" in role attributes, you'll have to
44 apply the attribute trait yourself:
45
46 has foo => (
47 traits => ['LazyRequire'],
48 is => 'ro',
49 lazy_required => 1,
50 );
51
53 Florian Ragwitz <rafl@debian.org>
54
56 This software is copyright (c) 2010 by Florian Ragwitz.
57
58 This is free software; you can redistribute it and/or modify it under
59 the same terms as the Perl 5 programming language system itself.
60
61
62
63perl v5.12.1 2010-07-19 MooseX::LazyRequire(3)