File Coverage

blib/lib/AppConfig/Exporter.pm
Criterion Covered Total %
statement 37 38 97.3
branch 7 12 58.3
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 55 61 90.1


line stmt bran cond sub pod time code
1             package AppConfig::Exporter;
2              
3 4     4   75372 use warnings;
  4         8  
  4         322  
4 3     3   22 use strict;
  3         7  
  3         121  
5 3     3   44 use Exporter;
  3         21  
  3         144  
6 3     3   3179 use AppConfig qw(:argcount);
  3         19883  
  3         1168  
7              
8             =head1 NAME
9              
10             AppConfig::Exporter - Allow modules to import AppConfig sections from a shared configuration.
11              
12             =cut
13              
14             our $VERSION = q(1.5);
15              
16             =head1 SYNOPSIS
17              
18             package MyConfig;
19             use base qw( AppConfig::Exporter );
20              
21             __PACKAGE__->configure( Config_File => 'myfile.conf',
22             AppConfig_Options => { CASE => 0 },
23             AppConfig_Define => { Fun_Pickles => {ARGCOUNT => ARGCOUNT_LIST} } );
24             1;
25              
26             =head1 USAGE
27              
28             B is intended to be subclassed to specify your configuration file and any options. Then, you can request a hash of any section from the configuration file by specifying it as a symbol to be imported in the B statement:
29              
30             # myfile.conf
31             [fruit]
32            
33             oranges = 'tasty'
34             apples = 'sweet'
35              
36             # some code elsewhere...
37              
38             use MyConfig qw(fruit);
39             print "$fruit{oranges}!"; # tasty!
40              
41             my $appconfig = MyConfig->AppConfig;
42              
43             =cut
44              
45             our @ISA = qw(Exporter);
46             our @EXPORT_OK;
47              
48             my $appconfig;
49              
50             =head1 CONFIGURATION
51              
52             =over
53              
54             =item configure
55              
56             This is how your class is initialized. You must specify a Config_File, and you may specify a hashref of AppConfig_Options and AppConfig_Define.
57              
58             =over
59              
60             =item Config_File
61              
62             Required - path to your AppConfig compatible config file
63              
64             __PACKAGE__->configure( Config_File => 'myfile.conf' );
65              
66              
67             =item AppConfig_Options
68              
69             Hash ref that will be fed to AppConfig - you can override this module's defaults, which are:
70              
71             CASE => 1,
72             CREATE => 1,
73             GLOBAL => {
74             ARGCOUNT => ARGCOUNT_ONE,
75             }
76              
77             For example:
78              
79             __PACKAGE__->configure( Config_File => 'myfile.conf',
80             AppConfig_Options => { CASE => 0 } );
81              
82             =item AppConfig_Define
83              
84             Hash ref that will be fed to AppConfig as a define statement if you wish for a specific variable to have different properties than the global ones.
85              
86             B So that you can use AppConfig's constants, this module automatically imports AppConfig's b<:argcount> tag into your package for you.
87              
88              
89             __PACKAGE__->configure( Config_File => 'myfile.conf',
90             AppConfig_Define => { Fun_Pickles => {ARGCOUNT => ARGCOUNT_LIST} } );
91              
92             =back
93              
94             =cut
95              
96             sub configure {
97 2     2 1 125 my $class = shift;
98 2         8 my %opts = @_;
99            
100 2 50       9 die "$class is already configured" if $appconfig;
101              
102 2 50       13 my $config_file = delete $opts{Config_File} or die __PACKAGE__ . ' requires a Config_File argment';
103              
104 0         0 $appconfig = AppConfig->new(
105             {
106             CASE => 1,
107             CREATE => 1,
108             GLOBAL => {
109             ARGCOUNT => ARGCOUNT_ONE,
110             },
111 1         6 defined $opts{AppConfig_Options} ? %{$opts{AppConfig_Options}} : (),
112             },
113 2 50       25 defined $opts{AppConfig_Define} ? %{$opts{AppConfig_Define}}: (),
    100          
114             );
115            
116 2 50       377 $appconfig->file( $config_file ) or die qq(Error reading config file "$config_file");
117             }
118              
119             =item import
120              
121             This does the heavy lifting using the Exporter. You don\'t call this directly - B will do it for you.
122              
123             =cut
124              
125             sub import {
126 4     4   4840 my $class = shift;
127 4         12 my @tags = @_;
128             {
129 3     3   32 no strict qw( refs ) ;
  3         6  
  3         494  
  4         7  
130 4         12 for my $section ( @tags ) {
131 2         7 push @EXPORT_OK, "\%$section";
132 2         16 *{"$section"} = { $appconfig->varlist("^${section}_", 1) };
  2         136  
133 2         189 __PACKAGE__->export_to_level( 1, $class, "\%$section" );
134             }
135             }
136 4         10 my $callpkg = caller(0);
137 3     3   18 eval "package $callpkg; use AppConfig qw(:argcount);";
  3         28  
  3         336  
  4         275  
138 4 50       68 die $@ if $@;
139             }
140              
141             =item AppConfig
142              
143             You can use this to access the the raw B object that the exporter sources for configuration.
144              
145             =back
146              
147             =cut
148              
149             sub AppConfig{
150 1     1 1 835 return $appconfig;
151             }
152              
153             =head1 AUTHOR
154              
155             Ben H Kram, C<< >>
156              
157             =head1 ACKNOWLEDGEMENTS
158              
159             Andy Wardley, for his excellent AppConfig module.
160              
161             =head1 COPYRIGHT & LICENSE
162              
163             Copyright 2007 Harvard University and Fellows, all rights reserved.
164              
165             This program is free software; you can redistribute it and/or modify it
166             under the same terms as Perl itself.
167              
168             =cut
169              
170             1; # End of AppConfig::Exporter