| 1 |
NAME |
| 2 |
CGI::Application::Plugin::ConfigAuto - Easy config file management for |
| 3 |
CGI::Application |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use CGI::Application::Plugin::ConfigAuto (qw/cfg/); |
| 7 |
|
| 8 |
In your instance script: |
| 9 |
|
| 10 |
my $app = WebApp->new(PARAMS => { cfg_file => 'config.pl' }); |
| 11 |
$app->run(); |
| 12 |
|
| 13 |
In your application module: |
| 14 |
|
| 15 |
sub my_run_mode { |
| 16 |
my $self = shift; |
| 17 |
|
| 18 |
# Access a config hash key directly |
| 19 |
$self->cfg('field'); |
| 20 |
|
| 21 |
# Return config as hash |
| 22 |
%CFG = $self->cfg; |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
DESCRIPTION |
| 27 |
CGI::Application::Plugin::ConfigAuto adds easy access to config file |
| 28 |
variables to your CGI::Application modules. Lazy loading is used to |
| 29 |
prevent the config file from being parsed if no configuration variables |
| 30 |
are accessed during the request. In other words, the config file is not |
| 31 |
parsed until it is actually needed. The Config::Auto package provides |
| 32 |
the framework for this plugin. |
| 33 |
|
| 34 |
RATIONALE |
| 35 |
"CGI::Application" promotes re-usable applications by moving a maximal |
| 36 |
amount of code into modules. For an application to be fully re-usable |
| 37 |
without code changes, it is also necessary to store configuration |
| 38 |
variables in a separate file. |
| 39 |
|
| 40 |
This plugin supports multiple config files for a single application, |
| 41 |
allowing config files to override each other in a particular order. This |
| 42 |
covers even complex cases, where you have a global config file, and |
| 43 |
second local config file which overrides a few variables. |
| 44 |
|
| 45 |
It is recommended that you to declare your config file locations in the |
| 46 |
instance scripts, where it will have minimum impact on your application. |
| 47 |
This technique is ideal when you intend to reuse your module to support |
| 48 |
multiple configuration files. If you have an application with multiple |
| 49 |
instance scripts which share a single config file, you may prefer to |
| 50 |
call the plugin from the setup() method. |
| 51 |
|
| 52 |
DECLARING CONFIG FILE LOCATIONS |
| 53 |
# In your instance script |
| 54 |
# value can also be an arrayref of config files |
| 55 |
my $app = WebApp->new(PARAMS => { cfg_file => 'config.pl' }) |
| 56 |
|
| 57 |
# OR ... |
| 58 |
|
| 59 |
# Pass in an array of config files, and they will be processed in order. |
| 60 |
$app->cfg_file('../../config/config.pl'); |
| 61 |
|
| 62 |
Your config files should be referenced using the syntax example above. |
| 63 |
Note that the key "config_files" can be used as alternative to cfg_file. |
| 64 |
|
| 65 |
The format is detected automatically using Config::Auto. It it known to |
| 66 |
support the following formats: colon separated, space separated, equals |
| 67 |
separated, XML, Perl code, and Windows INI. See that modules |
| 68 |
documentation for complete details. |
| 69 |
|
| 70 |
METHODS |
| 71 |
cfg() |
| 72 |
# Access a config hash key directly |
| 73 |
$self->cfg('field'); |
| 74 |
|
| 75 |
# Return config as hash |
| 76 |
my %CFG = $self->cfg; |
| 77 |
|
| 78 |
# return as hashref |
| 79 |
my $cfg_href = $self->cfg; |
| 80 |
|
| 81 |
A method to access project configuration variables. The config file is |
| 82 |
parsed on the first call with a perl hash representation stored in |
| 83 |
memory. Subsequent calls will use this version, rather than re-reading |
| 84 |
the file. |
| 85 |
|
| 86 |
In list context, it returns the configuration data as a hash. In scalar |
| 87 |
context, it returns the configuration data as a hashref. |
| 88 |
|
| 89 |
config() |
| 90 |
"config()" in CGI::Application::Standard::Config is provided as an alias |
| 91 |
to cfg() for compliance with CGI::Application::Standard::Config. It |
| 92 |
always exported by default per the standard. |
| 93 |
|
| 94 |
std_config() |
| 95 |
"std_config()" in CGI::Application::Standard::Config is implemented to |
| 96 |
comply with CGI::Application::Standard::Config. It's for developers. |
| 97 |
Users can ignore it. |
| 98 |
|
| 99 |
cfg_file() |
| 100 |
# Usual |
| 101 |
$self->cfg_file('my_config_file.pl'); |
| 102 |
|
| 103 |
# Supply the first format, guess the second |
| 104 |
$self->cfg_file('my_config_file.pl',{ format => 'perl' } ); |
| 105 |
|
| 106 |
Supply an array of config files, and they will be processed in order. If |
| 107 |
a hash reference if found it, will be used to supply the format for the |
| 108 |
previous file in the array. |
| 109 |
|
| 110 |
FILE FORMAT HINTS |
| 111 |
Perl |
| 112 |
Here's a simple example of my favorite config file format: Perl. Having |
| 113 |
the "shebang" line at the top helps "Config::Auto" to identify it as a |
| 114 |
Perl file. Also, be sure that your last statement returns a hash |
| 115 |
reference. |
| 116 |
|
| 117 |
#!/usr/bin/perl |
| 118 |
|
| 119 |
my %CFG = (); |
| 120 |
|
| 121 |
# directory path name |
| 122 |
$CFG{DIR} = '/home/mark/www'; |
| 123 |
|
| 124 |
# website URL |
| 125 |
$CFG{URL} = 'http://mark.stosberg.com/'; |
| 126 |
|
| 127 |
\%CFG; |
| 128 |
|
| 129 |
SEE ALSO |
| 130 |
CGI::Application CGI::Application::Plugin::ValidateRM |
| 131 |
CGI::Application::Plugin::DBH CGI::Application::Standard::Config. |
| 132 |
perl(1) |
| 133 |
|
| 134 |
AUTHOR |
| 135 |
Mark Stosberg "[email protected]" |
| 136 |
|
| 137 |
LICENSE |
| 138 |
Copyright (C) 2004 - 2011 Mark Stosberg "[email protected]" |
| 139 |
|
| 140 |
This library is free software. You can modify and or distribute it under |
| 141 |
the same terms as Perl itself. |
| 142 |
|