README.md
1# NAME
2
3MooX::Attribute::ENV - Allow Moo attributes to get their values from %ENV
4
5# PROJECT STATUS
6
7| OS | Build status |
8|:-------:|--------------:|
9| Linux | [![Build Status](https://travis-ci.org/mohawk2/moox-attribute-env.svg?branch=master)](https://travis-ci.org/mohawk2/moox-attribute-env) |
10
11[![CPAN version](https://badge.fury.io/pl/moox-attribute-env.svg)](https://metacpan.org/pod/MooX::Attribute::ENV) [![Coverage Status](https://coveralls.io/repos/github/mohawk2/moox-attribute-env/badge.svg?branch=master)](https://coveralls.io/github/mohawk2/moox-attribute-env?branch=master)
12
13# SYNOPSIS
14
15 package MyMod;
16 use Moo;
17 use MooX::Attribute::ENV;
18 # look for $ENV{attr_val} and $ENV{ATTR_VAL}
19 has attr => (
20 is => 'ro',
21 env_key => 'attr_val',
22 );
23 # look for $ENV{attr_val} and $ENV{next_val}, in that order
24 has some => (
25 is => 'ro',
26 env_key => [ 'attr_val', 'next_val' ],
27 );
28 # looks for $ENV{otherattr} and $ENV{OTHERATTR}, then any default
29 has otherattr => (
30 is => 'ro',
31 env => 1,
32 default => 7,
33 );
34 # looks for $ENV{xxx_prefixattr} and $ENV{XXX_PREFIXATTR}
35 has prefixattr => (
36 is => 'ro',
37 env_prefix => 'xxx',
38 );
39 # looks for $ENV{MyMod_packageattr} and $ENV{MYMOD_PACKAGEATTR}
40 has packageattr => (
41 is => 'ro',
42 env_package_prefix => 1,
43 );
44
45 $ perl -MMyMod -E 'say MyMod->new(attr => 2)->attr'
46 # 2
47 $ ATTR_VAL=3 perl -MMyMod -E 'say MyMod->new->attr'
48 # 3
49 $ OTHERATTR=4 perl -MMyMod -E 'say MyMod->new->otherattr'
50 # 4
51
52# DESCRIPTION
53
54This is a [Moo](https://metacpan.org/pod/Moo) extension. It allows other attributes for ["has" in Moo](https://metacpan.org/pod/Moo#has). If
55any of these are given, then ["BUILDARGS" in Moo](https://metacpan.org/pod/Moo#BUILDARGS) is wrapped so that values
56for object attributes can, if not supplied in the normal construction
57process, come from the environment.
58
59The environment will be searched for either the given case, or upper case,
60version of the names discussed below.
61
62When a prefix is mentioned, it will be prepended to the mentioned name,
63with a `_` in between.
64
65# ADDITIONAL ATTRIBUTES
66
67## env
68
69Boolean. If true, the name is the attribute, no prefix.
70
71## env\_key
72
73String. If true, the name is the given value, no prefix.
74
75or
76
77ArrayRef. A list of names that will be checked in given order.
78
79## env\_prefix
80
81String. The prefix is the given value.
82
83## env\_package\_prefix
84
85Boolean. If true, use as the prefix the current package-name, with `::`
86replaced with `_`.
87
88# AUTHOR
89
90Ed J, porting John Napiorkowski's excellent [MooseX::Attribute::ENV](https://metacpan.org/pod/MooseX%3A%3AAttribute%3A%3AENV).
91
92# LICENCE
93
94The same terms as Perl itself.
95