File Coverage

blib/lib/CGI/Application/Plugin/Config/Simple.pm
Criterion Covered Total %
statement 45 46 97.8
branch 13 16 81.2
condition 3 3 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 73 77 94.8


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::Config::Simple;
2 1     1   2053 use strict;
  1         2  
  1         50  
3 1     1   7 use warnings;
  1         2  
  1         145  
4 1     1   106 use base 'Exporter';
  1         2  
  1         111  
5 1     1   2709 use CGI::Application;
  1         19046  
  1         44  
6 1     1   108101 use Config::Simple;
  1         31894  
  1         18  
7              
8             $CGI::Application::Plugin::Config::Simple::VERSION = '1.01';
9 1     1   66 use vars '@EXPORT';
  1         1  
  1         497  
10             @EXPORT = qw(config_file config_param config);
11              
12             =pod
13              
14             =head1 NAME
15              
16             CGI::Application::Plugin::Config::Simple - Add Config::Simple support to CGI::Application
17              
18             =head1 SYNOPSIS
19              
20             in your CGI::Application based module
21              
22             use CGI::Application::Plugin::Config::Simple;
23              
24             sub cgiapp_init {
25             my $self = shift;
26             #set my config file
27             $self->config_file('myapp.conf');
28              
29             #
30             #do other stuff
31             #
32             }
33              
34             #later on in a run mode
35             sub run_mode1 {
36             my $self = shift;
37              
38             #just get a single parameter from my config file
39             my $value = $self->config_param('my_param');
40              
41             #get a parameter in a block (if using ini style files)
42             $value = $self->config_param('my_block.my_param');
43              
44             #the entire config hash reference
45             my $config_vars = $self->config_param();
46              
47             #get my Config::Simple object for direct access
48             my $config = $self->config;
49             }
50              
51              
52             =head1 DESCRIPTION
53              
54             This module acts as a plugin for L to be easily used inside of a
55             L module. It does not provide every method available from L
56             but rather easy access to your configuration variables. It does however provide direct
57             access to the underlying L object created if you want to use it's full
58             power.
59              
60             The module tries to make the getting and setting of configuration variables as easy as
61             possible. Only three methods are exported into your L module and they
62             are described below.
63              
64             Before I wrote this module sometimes I would put my code that read in the configuration file
65             into the cgiapp_init() or cgiapp_prerun() methods but then if I had a run mode that didn't need
66             those config variables it was run anyway. This module helps to solve this is. The L
67             object is not created (and the config file is not read and parsed) until after your first call
68             to L or L to either retrieve/set values, or get the L
69             object. This lazy loading idea came from Cees Hek's L module.
70              
71             =head1 METHODS
72              
73             =head2 config_param()
74            
75             This method acts as an accessor/mutator for configuration variables coming from the
76             configuration file.
77            
78             This method will behave in three different ways depending on how many parameters it
79             is passed. If 0 parameters are passed then the entire config structure will be returned
80             as a hash ref. If 1 parameters is passed then the value of that parameter in the config
81             file will be returned. If more than 1 parameter is passed then it will treat them as name
82             value pairs and will set the parameters in the config file accordingly. In this case, if
83             we successfully set the parameters then a true value will be returned.
84            
85             #get the complete config hash
86             my $config_hash = $self->config_param();
87             #just get one config value
88             my $value = $self->config_param($parameter);
89             #set multiple config values
90             my $success = $self->config_param(param1 => $value1, param2 => $value2);
91            
92             This method uses Config::Simple so if you are using ini-files then you can set
93             the values of variables inside blocks as well using the '.' notation. See L;
94              
95             You must set the name of the configuration file either using the L method
96             or the CGIAPP_CONFIG_FILE environment variable before calling this method or it will 'die'.
97              
98             =cut
99            
100             sub config_param {
101 10     10 1 3975 my $self = shift;
102 10         25 my @params = @_;
103 10         46 my $conf = $self->config();
104            
105             #if there aren't any params then we want the entire config structure as a hash ref
106 10 100       41 if(scalar(@params) == 0) {
    100          
107 1         13 return scalar($conf->vars)
108             } elsif(scalar(@params) == 1) {
109             #if there is just one then we want just that value
110 7         30 return $conf->param($params[0])
111             }
112             #else we might be setting some values
113             else {
114 2         8 my %params = (@params);
115 2         13 $conf->param($_ => $params{$_}) foreach (keys %params);
116 2 50       211 if($conf->write) {
117 2         769 return 1
118             } else {
119 0         0 die "Config-Plugin: Could not write to config file (" . $self->config_file . ")! " . $conf->error()
120             }
121             }
122             }
123              
124              
125             =pod
126            
127             =head2 config()
128            
129             This method will return the underlying Config::Simple object for more direct use by your
130             application. You must set the name of the configuration file either using the L method
131             or the CGIAPP_CONFIG_FILE environment variable before calling this method or it will 'die'.
132            
133             my $conf = $self->config();
134            
135             =cut
136            
137             sub config {
138 11     11 1 420 my $self = shift;
139             #if we don't already have a config object or if the file name has changed on us then create it
140 11   100     71 my $create = !$self->{__CONFIG_SIMPLE}->{__CONFIG_OBJ}
141             || $self->{__CONFIG_SIMPLE}->{__FILE_CHANGED};
142 11 100       28 if($create) {
143             #get the file name from config_file()
144 2 50       5 my $file_name = $self->config_file or die "No config file specified!";
145 2 50       14 my $conf = Config::Simple->new($file_name) or die "Could not create Config::Simple object for file $file_name! $!";
146 2         9553 $self->{__CONFIG_SIMPLE}->{__CONFIG_OBJ} = $conf;
147 2         11 $self->{__CONFIG_SIMPLE}->{__FILE_CHANGED} = 0;
148             }
149 11         46 return $self->{__CONFIG_SIMPLE}->{__CONFIG_OBJ};
150             }
151            
152             =pod
153            
154             =head2 config_file([$file_name])
155            
156             This method acts as an accessor/mutator to either get the name of the current config file
157             or to change/initialize it. This method must be called to initialize the name of the config
158             file before any call can be made to either L or L unless the
159             'CGIAPP_CONFIG_FILE' environment variable has been set. If this environment variable is set
160             it will be used as the initial value of the config file. This is useful if we are running in
161             a mod_perl environment when can use a statement like this in your httpd.conf file:
162            
163             PerlSetEnv CGIAPP_CONFIG_FILE /path/to/my/conf
164              
165             It is typical to set the name of the config file in the cgiapp_init() phase of your application.
166            
167             If a value is passed as a parameter then the config file with that name is used. It will always
168             return the name of the current config file.
169            
170             #get the value of the CGIAPP_CONFIG_FILE environment variable (if there is one)
171             #since we haven't set the config file's name with config_file() yet.
172             my $file_name = $self->config_file();
173            
174             #set the config file's name
175             $self->config_file('myapp.conf');
176            
177             #get the name of the config file
178             $file_name = $self->config_file();
179            
180             =cut
181            
182             sub config_file {
183 4     4 1 2067 my ($self, $file_name) = @_;
184             #if we have a file name to set
185 4 100       13 if(defined $file_name) {
186 1         3 $self->{__CONFIG_SIMPLE}->{__FILE_NAME} = $file_name;
187 1         3 $self->{__CONFIG_SIMPLE}->{__FILE_CHANGED} = 1;
188             } else {
189             #else we are getting the filename
190 3         16 $file_name = $self->{__CONFIG_SIMPLE}->{__FILE_NAME}
191             }
192             #if we don't have the file_name then get it from %ENV, but untaint it
193 4 100       14 if(!$file_name) {
194 2         6 $ENV{CGIAPP_CONFIG_FILE} =~ /(.*)/;
195 2         9 $file_name = $1;
196             }
197 4         13 return $file_name;
198             }
199              
200             1;
201              
202             __END__