MooseX...e::ENV(3pm)

πŸ’– MooseX::Attribute::ENV πŸ’–

okay so bestie, welcome to MooseX::Attribute::ENV β€” a User Contributed Perl Documentation page (section 3pm, she's fancy) and literally the cutest way to make your attribute defaults come straight out of %ENV. pull up a chair. 🫢


✨ NAME ✨

MooseX::Attribute::ENV β€” set the default of an attribute to a value from %ENV. that's the whole job and she does it flawlessly. πŸ’…


🌸 SYNOPSIS 🌸

here's the vibe, bestie. this is example usage for this attribute trait:

package MyApp::MyClass;

use Moose;
use MooseX::Attribute::ENV;

## Checks $ENV{username} and $ENV{USERNAME}
has 'username' => (
    traits => ['ENV'],
);

## Checks $ENV{GLOBAL_PASSWORD}
has 'password' => (
    traits => ['ENV'],
    env_key => 'GLOBAL_PASSWORD',
);

## Checks $ENV{last_login}, $ENV{LAST_LOGIN} and then uses the default
has 'last_login' => (
    traits => ['ENV'],
    default => sub {localtime},
);

## Checks $ENV{XXX_config_name} and $ENV{XXX_CONFIG_NAME}
has 'config_name' => (
    traits => ['ENV'],
    env_prefix => 'XXX',
);

## Checks $ENV{MyApp_MyClass_extra} and $ENV{MYAPP_MYCLASS_EXTRA}
has 'extra' => (
    traits => ['ENV'],
    env_package_prefix => 1,
);

and please, please go peek at the test cases for more detailed examples. they ate. πŸ”₯


πŸ¦‹ DESCRIPTION πŸ¦‹

so this is a Moose attribute trait that you reach for when you want the default value of an attribute to be populated from the %ENV hash. iconic behaviour, honestly.

so like, for example, if you've set the environment variable USERNAME = 'John' you can do:

package MyApp::MyClass;

use Moose;
use MooseX::Attribute::ENV;

has 'username' => (is=>'ro', traits=>['ENV']);

package main;

my $myclass = MyApp::MyClass->new();

print $myclass->username; # STDOUT => 'John';

ngl, it's basically similar functionality to something like:

has 'attr' => (
    is=>'ro',
    default=> sub {
        $ENV{uc 'attr'};
    },
);

but this module has a few other features that offer merit, plus she's a simple enough attribute trait that the author hopes she can serve as a learning tool. main character energy, truly. πŸ‘‘

and the important bit: if the named key isn't found in %ENV, then defaults will execute as normal. no cap. ✨


πŸŽ€ ATTRIBUTES πŸŽ€

this role defines the following attributes.

env_key ($Str)

by default we look for a key in %ENV based on the actual attribute name. if you want or need to override this behaviour, you use this modifier. simple as. πŸ’–

env_prefix ($Str)

a prefix to attach to the generated filename. the prefix is prepended with a trailing underscore. for example, if your attribute was 'attr' and you set a prefix of 'xxx' then we'd check for $ENV{xxx_attr} and $ENV{XXX_ATTR}.

env_package_prefix ($Bool)

similar to env_prefix, but automatically sets the prefix based on the consuming class's package name. so if your attribute is 'attr' and it's in a package called 'Myapp::Myclass' the following keys in %ENV will be examined:

  • Myapp_Myclass_attr
  • MYAPP_MYCLASS_ATTR
⚠️ okay heads up bestie: please be aware that if you use this feature, your attribute will automatically be converted to lazy, which might affect any default subrefs you also assign to this attribute. 😭
🚨 and please note that you can't currently use this option along with the option 'lazy_build'. that might change in a future release, however since these attributes are likely to hold simple strings the lazy_build option probably won't be missed. πŸ’€

🧚 METHODS 🧚

this module defines the following methods.

_process_options

overload method so that we can assign the default to be what's in %ENV. slay. ✨


πŸ’œ AUTHOR πŸ’œ

John Napiorkowski, "<jjnapiork at cpan.org>"


🌈 BUGS 🌈

please report any bugs or feature requests to:

C<MooseX-Attribute-ENV at rt.cpan.org>

or through the web interface at:

L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Attribute-ENV>

i will be notified, and then you'll automatically be notified of progress on your bug as I make changes. we love a feedback loop. 🫢


⭐ SUPPORT ⭐

you can find documentation for this module with the perldoc command.

perldoc MooseX::Attribute::ENV

you can also look for information at:

  • RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attribute-ENV

  • AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/MooseX-Attribute-ENV

  • CPAN Ratings

http://cpanratings.perl.org/d/MooseX-Attribute-ENV

  • Search CPAN

http://search.cpan.org/dist/DBIx-Class-PopulateMore


πŸ“ LICENSE πŸ“

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. periodt. πŸ’…


and that's the whole page, bestie!! this one's perl v5.36.0, dated 2023-01-27, and it was giving User Contributed Perl Documentation the entire time. go set some env vars and thrive. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from