https://github.com/mohawk2/moox-attribute-env
Moo equivalent of https://metacpan.org/pod/MooseX::Attribute::ENV
https://github.com/mohawk2/moox-attribute-env
Last synced: 9 months ago
JSON representation
Moo equivalent of https://metacpan.org/pod/MooseX::Attribute::ENV
- Host: GitHub
- URL: https://github.com/mohawk2/moox-attribute-env
- Owner: mohawk2
- Created: 2019-03-14T22:21:30.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-10-19T15:28:48.000Z (over 3 years ago)
- Last Synced: 2025-03-30T03:43:02.563Z (about 1 year ago)
- Language: Perl
- Homepage: https://metacpan.org/pod/MooX::Attribute::ENV
- Size: 18.6 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: Changes
Awesome Lists containing this project
README
# NAME
MooX::Attribute::ENV - Allow Moo attributes to get their values from %ENV
# PROJECT STATUS
| OS | Build status |
|:-------:|--------------:|
| Linux | [](https://travis-ci.org/mohawk2/moox-attribute-env) |
[](https://metacpan.org/pod/MooX::Attribute::ENV) [](https://coveralls.io/github/mohawk2/moox-attribute-env?branch=master)
# SYNOPSIS
package MyMod;
use Moo;
use MooX::Attribute::ENV;
# look for $ENV{attr_val} and $ENV{ATTR_VAL}
has attr => (
is => 'ro',
env_key => 'attr_val',
);
# look for $ENV{attr_val} and $ENV{next_val}, in that order
has some => (
is => 'ro',
env_key => [ 'attr_val', 'next_val' ],
);
# looks for $ENV{otherattr} and $ENV{OTHERATTR}, then any default
has otherattr => (
is => 'ro',
env => 1,
default => 7,
);
# looks for $ENV{xxx_prefixattr} and $ENV{XXX_PREFIXATTR}
has prefixattr => (
is => 'ro',
env_prefix => 'xxx',
);
# looks for $ENV{MyMod_packageattr} and $ENV{MYMOD_PACKAGEATTR}
has packageattr => (
is => 'ro',
env_package_prefix => 1,
);
$ perl -MMyMod -E 'say MyMod->new(attr => 2)->attr'
# 2
$ ATTR_VAL=3 perl -MMyMod -E 'say MyMod->new->attr'
# 3
$ OTHERATTR=4 perl -MMyMod -E 'say MyMod->new->otherattr'
# 4
# DESCRIPTION
This is a [Moo](https://metacpan.org/pod/Moo) extension. It allows other attributes for ["has" in Moo](https://metacpan.org/pod/Moo#has). If
any of these are given, then ["BUILDARGS" in Moo](https://metacpan.org/pod/Moo#BUILDARGS) is wrapped so that values
for object attributes can, if not supplied in the normal construction
process, come from the environment.
The environment will be searched for either the given case, or upper case,
version of the names discussed below.
When a prefix is mentioned, it will be prepended to the mentioned name,
with a `_` in between.
# ADDITIONAL ATTRIBUTES
## env
Boolean. If true, the name is the attribute, no prefix.
## env\_key
String. If true, the name is the given value, no prefix.
or
ArrayRef. A list of names that will be checked in given order.
## env\_prefix
String. The prefix is the given value.
## env\_package\_prefix
Boolean. If true, use as the prefix the current package-name, with `::`
replaced with `_`.
# AUTHOR
Ed J, porting John Napiorkowski's excellent [MooseX::Attribute::ENV](https://metacpan.org/pod/MooseX%3A%3AAttribute%3A%3AENV).
# LICENCE
The same terms as Perl itself.