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 version 0.11
11
13 package Foo;
14
15 use Moose;
16 use MooseX::LazyRequire;
17
18 has foo => (
19 is => 'ro',
20 lazy_required => 1,
21 );
22
23 has bar => (
24 is => 'ro',
25 builder => '_build_bar',
26 );
27
28 sub _build_bar { shift->foo }
29
30
31 Foo->new(foo => 42); # succeeds, foo and bar will be 42
32 Foo->new(bar => 42); # succeeds, bar will be 42
33 Foo->new; # fails, neither foo nor bare were given
34
36 This module adds a "lazy_required" option to Moose attribute
37 declarations.
38
39 The reader methods for all attributes with that option will throw an
40 exception unless a value for the attributes was provided earlier by a
41 constructor parameter or through a writer method.
42
44 Prior to Moose 1.9900, roles didn't have an attribute metaclass, so
45 this module can't easily apply its magic to attributes defined in
46 roles. If you want to use "lazy_required" in role attributes, you'll
47 have to apply the attribute trait yourself:
48
49 has foo => (
50 traits => ['LazyRequire'],
51 is => 'ro',
52 lazy_required => 1,
53 );
54
55 With Moose 1.9900, you can use this module in roles just the same way
56 you can in classes.
57
59 · Florian Ragwitz <rafl@debian.org>
60
61 · Dave Rolsky <autarch@urth.org>
62
64 This software is copyright (c) 2009 by Florian Ragwitz.
65
66 This is free software; you can redistribute it and/or modify it under
67 the same terms as the Perl 5 programming language system itself.
68
70 · Karen Etheridge <ether@cpan.org>
71
72 · David Precious <davidp@preshweb.co.uk>
73
74 · Jesse Luehrs <doy@tozt.net>
75
76
77
78perl v5.32.0 2020-07-28 MooseX::LazyRequire(3)