1 package Catalyst::Plugin::ConfigLoader::Environment;
9 Catalyst::Plugin::ConfigLoader::Environment - Configure your
10 application with environment variables.
18 our $VERSION = '0.06';
22 Catalyst::Plugin::ConfigLoader::Environment reads environment
23 variables and sets up the configuration in your application
26 Here's how you use it:
29 use Catalyst qw(... ConfigLoader::Environment ...);
32 Then, before you run your application, set some environment
35 export MYAPP_foo='Hello, world!'
36 export MYAPP_bar="foobar"
37 perl script/myapp_server.pl
39 Inside your application, C<< $c->config->{foo} >> will be equal to
40 C<Hello, world!>, and C<< $c->config->{bar} >> will be equal to
43 =head2 Compatibility with ConfigLoader
45 You can use both ConfigLoader and this module in the same application.
46 If you specify C<ConfigLoader> before C<ConfigLoader::Environment> in
47 the import list to Catalyst, the environment will override any
48 configuration files that ConfigLoader may find. This is the
51 You can reverse the order in the import list if you want static config
52 files to override the environment, but that's not recommended.
56 Here's exactly how environment variables are translated into
59 First, your application's name is converted to ALL CAPS, any colons
60 are converted to underscores (i.e. C<My::App> is translated to
61 C<MY_APP>), and a C<_> is appended. Then, any environment variables
62 not starting with this prefix are discarded.
64 The prefix is then stripped, and the remaining variables are then
65 converted to elements in the C<config> hash as follows.
67 Variables starting with C<Model::>, C<View::>, or C<Controller::> and
68 then any character other than C<_> are treated as configuration
69 options for the corresponding component of your application. The
70 prefix is saved as C<prefix> and everything after the first C<_> is
71 used as a key into the C<< $c->config->{"prefix"} >> hash.
73 Any other variables not starting with a special prefix are added
74 directly to the C<< $c->config >> hash.
78 Let's translate a YAML config file:
82 title: This is My App!
89 connect_info: [ "dbi:Pg:dbname=foo", "username", "password" ]
91 into environment variables that would setup the same configuration:
94 MYAPP_title=This is My App!
95 MYAPP_View__Foo_EXTENSION=tt
96 MYAPP_View__Foo_EVAL_PERL=1
97 MYAPP_Model__Bar_root=/etc
98 MYAPP_Model__DBIC_connect_info=["dbi:Pg:dbname=foo", "username", "password"]
100 Double colons are converted into double underscores. For
101 compatibility's sake, support for the 0.01-style use of
102 bourne-incompatible variable names is retained.
104 Values are JSON-decoded if they look like JSON arrays or objects
105 (i.e. if they're enclosed in []s or {}s). Taking advantage of that, we
106 can write the same example this way:
109 MYAPP_title=This is My App!
110 MYAPP_View__Foo={"EXTENSION":"tt","EVAL_PERL":1}
111 MYAPP_Model__Bar={"root":"/etc"}
112 MYAPP_Model__DBIC={"connect_info":["dbi:Pg:dbname=foo", "username", "password"]}
118 Overriding Catalyst' setup routine.
124 my $prefix = Catalyst::Utils::class2env($c);
126 grep { /^${prefix}[_](.+)$/ && ($env{$1}=$ENV{$_})} keys %ENV;
128 foreach my $var (keys %env) {
129 my $val = $env{$var};
131 # Decode JSON array/object
132 if ( $val =~ m{^\[.*\]$|^\{.*\}$} ) {
133 $val = JSON::Any->jsonToObj($val);
136 # Special syntax Model__Foo is equivalent to Model::Foo
137 if($var =~ /(Model|View|Controller)(?:::|__)([^_]+)(?:_(.+))?$/) {
140 # Special syntax Model__Foo_bar (or Model::Foo_bar) will
141 # tweak just the 'bar' subparam for Model::Foo's
142 # config. We can accomplish this using a hash that
143 # specifies just 'bar' ($c->config will merge hashes).
145 $val = { $3 => $val };
149 $c->config( $var => $val );
152 return $c->maybe::next::method(@_);
158 Jonathan Rockway, C<< <jrockway at cpan.org> >>
164 Ryan D Johnson, C<< <ryan at innerfence.com> >>
168 Please report any bugs or feature requests to
169 C<bug-catalyst-plugin-configloader-environment at rt.cpan.org>, or through the web interface at
170 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-ConfigLoader-Environment>.
171 I will be notified, and then you'll automatically be notified of progress on
172 your bug as I make changes.
176 You can find documentation for this module with the perldoc command.
178 perldoc Catalyst::Plugin::ConfigLoader::Environment
180 You can also look for information at:
184 =item * Catalyst Mailing List
186 L<mailto:catalyst@lists.rawmode.org>.
188 =item * RT: CPAN's request tracker
190 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-ConfigLoader-Environment>
194 =head1 COPYRIGHT & LICENSE
196 Copyright 2006 Jonathan Rockway, all rights reserved.
198 This program is free software; you can redistribute it and/or modify it
199 under the same terms as Perl itself.
201 If you'd like to use it under a different license, that's probably OK.
202 Please contact the author.
206 1; # End of Catalyst::Plugin::ConfigLoader::Environment