File: ENV.pm

package info (click to toggle)
libmoosex-attribute-env-perl 0.02-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: perl: 2,059; makefile: 8
file content (261 lines) | stat: -rw-r--r-- 5,801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package MooseX::Attribute::ENV;

use Moose::Role;

our $VERSION = "0.02";
our $AUTHORITY = 'cpan:JJNAPIORK';

=head1 NAME

MooseX::Attribute::ENV - Set default of an attribute to a value from %ENV

=head1 SYNOPSIS

The following 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,
	);

Please see the test cases for more detailed examples.

=head1 DESCRIPTION

This is a L<Moose> attribute trait that you use when you want the default value
for an attribute to be populated from the %ENV hash.  So, for example if you
have 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';

This is 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, as well as being a
simple enough attribute trait that I hope it can serve as a learning tool.

If the named key isn't found in %ENV, then defaults will execute as normal.

=head1 ATTRIBUTES

This role defines the following attributes.

=head2 env_key ($Str)

By default we look for a key in %ENV based on the actual attribute name.  If
want or need to override this behavior, you can use this modifier.

=cut

has 'env_key' => (
	is=>'ro',
	isa=>'Str',
	predicate=>'has_env_key',
);

=head2 env_prefix ($Str)

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

=cut

has 'env_prefix' => (
	is=>'ro',
	isa=>'Str',
	predicate=>'has_env_prefix',
);

=head2 env_package_prefix ($Bool)

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

* Myapp_Myclass_attr
* MYAPP_MYCLASS_ATTR

Please be aware that if you use this feature, your attribute will automatically
be converted to lazy, which might effect any default subrefs you also assign to
this attribute.

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.

=cut

has 'env_package_prefix' => (
	is=>'ro',
	isa=>'Str',
	predicate=>'has_env_package_prefix',
);

=head1 METHODS

This module defines the following methods.

=head2 _process_options

Overload method so that we can assign the default to be what's in %ENV

=cut

around '_process_options' => sub
{
    my ($_process_options, $self, $name, $options) = (shift, @_);

    ## get some stuff we need.
	my $key = $options->{env_key} || $name;
	my $default = $options->{default};
	my $use_pp = $options->{env_package_prefix};

	## Make it lazy if we are using the package prefix option
	if( defined $use_pp && $use_pp )
	{
		$options->{lazy} = 1;
	}

	## Prepend any custom prefixes.
	if($options->{env_prefix})
	{
		$key = join('_', ($options->{env_prefix}, $key));
	}

	## override/update the default method for this attribute.
	CHECK_ENV: {

		$options->{default} = sub {

			if(defined $use_pp && $use_pp)
			{
				my $class = blessed $_[0];
				$class =~s/::/_/g;

				$key = join ('_', ($class, $key));
			}

			## Wish we could use perl 5.10 given instead :)
			if(defined $ENV{$key})
			{
				return $ENV{$key};
			}
			elsif(defined $ENV{uc $key})
			{
				return $ENV{uc $key};
			}
			elsif(defined $default)
			{
				return ref $default eq 'CODE' ? $default->(@_) : $default;
			}
		};
	}

    $_process_options->($self, $name, $options);
};

=head1 AUTHOR

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

=head1 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.

=head1 SUPPORT

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

    perldoc MooseX::Attribute::ENV

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

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

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/MooseX-Attribute-ENV>

=item * CPAN Ratings

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

=item * Search CPAN

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

=back

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

## Register the trait so this can be used without verbose invocation.
package Moose::Meta::Attribute::Custom::Trait::ENV;
sub register_implementation { 'MooseX::Attribute::ENV' }

1;