File Coverage

blib/lib/Config/Plugin/TinyManifold.pm
Criterion Covered Total %
statement 23 23 100.0
branch 3 6 50.0
condition 1 2 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 34 38 89.4


line stmt bran cond sub pod time code
1             package Config::Plugin::TinyManifold;
2              
3 2     2   3067 use strict;
  2         13  
  2         56  
4 2     2   11 use warnings;
  2         4  
  2         60  
5              
6 2     2   12 use Carp;
  2         4  
  2         209  
7              
8 2     2   1013 use Config::Tiny;
  2         2222  
  2         78  
9              
10 2     2   13 use vars qw(@EXPORT @ISA);
  2         5  
  2         522  
11              
12             @EXPORT = ('config_manifold');
13             @ISA = ('Exporter');
14              
15             our $VERSION = '1.02';
16              
17             # --------------------------------------------------
18              
19             sub config_manifold
20             {
21 1     1 1 131 my($self, $file_name) = @_;
22 1   50     4 $file_name ||= '';
23              
24             # Check [global].
25              
26 1         8 my($config) = Config::Tiny -> read($file_name);
27              
28 1 50       290 croak 'Error: ' . Config::Tiny -> errstr . "\n" if (Config::Tiny -> errstr);
29              
30 1         8 my($section) = 'global';
31 1 50       5 $section = $$config{$section}{'section'} if ($$config{$section});
32              
33 1 50       5 croak "Error: Config file '$file_name' does not contain the section '$section'\n" if (! $$config{$section});
34              
35 1         9 return $$config{$section};
36              
37             } # End of config_manifold.
38              
39             # --------------------------------------------------
40              
41             1;
42              
43             =pod
44              
45             =head1 NAME
46              
47             Config::Plugin::TinyManifold - A plugin which uses Config::Tiny with 1 of N sections
48              
49             =head1 Synopsis
50              
51             package My::App;
52              
53             use strict;
54             use warnings;
55              
56             use Config::Plugin::TinyManifold; # For config_manifold().
57              
58             use File::Spec;
59              
60             # ------------------------------------------------
61              
62             sub marine
63             {
64             my($self) = @_;
65             my($config) = $self -> config_manifold(File::Spec -> catfile('some', 'dir', 'config.tiny.manifold.ini') );
66              
67             } # End of marine.
68              
69             # --------------------------------------------------
70              
71             1;
72              
73             t/config.tiny.manifold.ini ships with the L distro, and is used in the test file t/test.t.
74              
75             =head1 Description
76              
77             When you 'use' this module (as in the Synopsis), it automatically imports into your class the method L to give you access to config data stored in an *.ini file.
78              
79             But more than that, it uses a value from the config file to select 1 of N sections within that file as the whole config. See L for details.
80              
81             =head1 Distributions
82              
83             This module is available as a Unix-style distro (*.tgz).
84              
85             See L
86             for help on unpacking and installing distros.
87              
88             =head1 Installation
89              
90             Install L as you would for any C module:
91              
92             Run:
93              
94             cpanm Config::Plugin::TinyManifold
95              
96             or run:
97              
98             sudo cpan Config::Plugin::TinyManifold
99              
100             or unpack the distro, and then either:
101              
102             perl Build.PL
103             ./Build
104             ./Build test
105             sudo ./Build install
106              
107             or:
108              
109             perl Makefile.PL
110             make (or dmake or nmake)
111             make test
112             make install
113              
114             =head1 Constructor and Initialization
115              
116             This module does not have, and does not need, a constructor.
117              
118             =head1 Methods
119              
120             =head2 config_manifold($file_name)
121              
122             Returns a *.ini-style config file as a hashref.
123              
124             Here, the [] indicate an optional parameter.
125              
126             The $file_name is passed to L's read($file_name) method.
127              
128             It uses a value from the config file to select 1 of N sections within that file as the whole config.
129              
130             Specifically, the name of the global section is hard-coded as '[global]', and the name of the key within that is hard-coded as 'section'.
131              
132             So, a sample config file, which ships as t/config.tiny.manifold.ini is:
133              
134             [global]
135              
136             # The 'section' key:
137             # o Specifies which section to use after the [global] section ends.
138             # o Case-sensitive options are /^(localhost|webhost)$/.
139              
140             section = localhost
141              
142             [localhost]
143              
144             template_path = /home/ron/assets/templates/CGI/Snapp
145              
146             [website]
147              
148             template_path = /dev/shm/html/assets/templates/CGI/Snapp
149              
150             Alternately, you could use sections called [global], [testing] and [production], and so on.
151              
152             =head1 FAQ
153              
154             =head2 When would I use this module?
155              
156             In your sub-class of L for example, or anywhere else you want effortless access to a *.ini file which contains N alternate sections, and you wish to auto-select 1 of these sections.
157              
158             For example, if you wish to load a config for use by a module such as L, try L or Config::Plugin::TinyManifold.
159              
160             =head2 Why didn't you call the exported method config()?
161              
162             Because L allows both L and L to be used in the same code.
163              
164             =head2 Why don't you 'use Exporter;'?
165              
166             It is not needed; it would be for documentation only.
167              
168             For the record, Exporter V 5.567 ships with Perl 5.8.0. That's what I had in Build.PL and Makefile.PL until I tested the fact I can omit it.
169              
170             =head2 What's the error message format?
171              
172             Every message passed to croak matches /^Error/ and ends with "\n".
173              
174             =head2 What does 'manifold' mean, exactly?
175              
176             My paper dictionary lists 8 meanings. The first 2 are:
177              
178             =over 4
179              
180             =item 1: of many kinds; numerous and varied: I
181              
182             =item 2: having many different parts, elements, features, forms, etc.
183              
184             =back
185              
186             So, rather like sections in a *.ini file...
187              
188             =head1 Repository
189              
190             L
191              
192             =head1 See Also
193              
194             L
195              
196             The following are all part of this set of distros:
197              
198             L - A almost back-compat fork of CGI::Application
199              
200             L - A template-free demo of CGI::Snapp using just 1 run mode
201              
202             L - A template-free demo of CGI::Snapp using N run modes
203              
204             L - A template-free demo of CGI::Snapp using CGI::Snapp::Plugin::Forward
205              
206             L - A template-free demo of CGI::Snapp using Log::Handler::Plugin::DBI
207              
208             L - A wrapper around CGI::Snapp::Demo::Four, to simplify using Log::Handler::Plugin::DBI
209              
210             L - A plugin which uses Config::Tiny
211              
212             L - A plugin which uses Config::Tiny with 1 of N sections
213              
214             L - Persistent session data management
215              
216             L - A plugin for Log::Handler using Log::Hander::Output::DBI
217              
218             L - A helper for Log::Hander::Output::DBI to create your 'log' table
219              
220             =head1 Machine-Readable Change Log
221              
222             The file CHANGES was converted into Changelog.ini by L.
223              
224             =head1 Version Numbers
225              
226             Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
227              
228             =head1 Credits
229              
230             Please read L, since a lot of the ideas for this module were copied from
231             L.
232              
233             =head1 Support
234              
235             Bugs should be reported via the CPAN bug tracker at
236              
237             L
238              
239             =head1 Author
240              
241             L was written by Ron Savage Iron@savage.net.auE> in 2012.
242              
243             Home page: L.
244              
245             =head1 Copyright
246              
247             Australian copyright (c) 2012, Ron Savage.
248              
249             All Programs of mine are 'OSI Certified Open Source Software';
250             you can redistribute them and/or modify them under the terms of
251             The Artistic License, a copy of which is available at:
252             http://www.opensource.org/licenses/index.html
253              
254             =cut