File Coverage

blib/lib/Config/DotNetXML.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Config::DotNetXML;
2              
3             =head1 NAME
4              
5             Config::DotNetXML - Get config in the manner of .NET Framework
6              
7             =head1 ABSTRACT
8              
9             This module allows the transparent import of .NET Framework style config.
10              
11             =head1 SYNOPSIS
12              
13             use Config::DotNetXML;
14              
15             our %appSettings;
16              
17             my $foo = $appSettings{Foo};
18              
19             =head1 DESCRIPTION
20              
21             This module attempts to provide a configuration facility similar to that
22             provided by System.Configuration.ConfigurationSettings class in the .NET
23             framework, the intent is that .NET programs and Perl programs can share the
24             same configuration file.
25              
26             When the modules import() method is called (either implicitly via use or
27             explicitly) it will read and parse and XML file (by default called $0.config)
28             in the same directory as the script and import the settings specified in
29             that file into the %appSettings hash in the current package.
30              
31             The XML file is of the format:
32              
33            
34            
35            
36            
37            
38              
39             The EaddEelements are the ones that contain the configuration, with
40             the 'key' attribute becoming the key in %appSettings and 'value' becoming
41             the value.
42              
43             The default behaviour of the module can be altered by the following parameters
44             that are supplied via the import list of the module:
45              
46             =over 2
47              
48             =item Package
49              
50             Alter the package into which the settings are imported. The default is the
51             package in which C is called.
52              
53             =item VarName
54              
55             Use a different name for the variable into which the settings are placed.
56             The default is C<%appSettings>, the name should not have the type specifier.
57              
58             =item File
59              
60             Use a different filename from which to get the settings. The default is the
61             program name with '.config' appended.
62              
63             =item Section
64              
65             By default the configuration is taken from the 'appSettings' section of the
66             file - however this can be changed by this parameter.
67             See L for details on named sections.
68              
69             =back
70              
71             If you don't want or need the import you should use the L
72             module which is part of this package instead.
73              
74             =cut
75              
76 7     7   17517 use warnings;
  7         18  
  7         452  
77 7     7   45 use strict;
  7         14  
  7         541  
78 7     7   42 use File::Spec;
  7         16  
  7         255  
79 7     7   4381 use Config::DotNetXML::Parser;
  0            
  0            
80              
81             BEGIN
82             {
83             delete $INC{'FindBin.pm'};
84             require FindBin;
85             }
86              
87             use vars qw($VERSION);
88              
89             $VERSION = '1.5';
90              
91             sub import
92             {
93              
94             my ($pkg, %Args ) = @_;
95              
96             no strict 'refs';
97              
98             my $suffix = '.config';
99              
100              
101             my $package;
102              
103             if ( exists $Args{Package} )
104             {
105             $package = $Args{Package};
106             }
107             else
108             {
109              
110             if ( ($package = caller(0)) eq 'Test::More')
111             {
112             $package = caller(1);
113             }
114             }
115              
116            
117             my $varname;
118              
119             if ( exists $Args{VarName} )
120             {
121             $varname = $Args{VarName};
122             }
123             else
124             {
125             $varname = 'appSettings';
126             }
127              
128             my $file;
129              
130             if ( exists $Args{File} )
131             {
132             $file = $Args{File};
133             }
134             else
135             {
136             $file = File::Spec->catfile($FindBin::Bin,$FindBin::Script) . $suffix;
137             }
138              
139              
140             my $section = 'appSettings';
141              
142             if ( exists $Args{Section} )
143             {
144             $section = $Args{Section}
145             }
146              
147             my $parser;
148              
149             my $appsettings = {};
150             if ( -f $file and -r $file )
151             {
152             $parser = Config::DotNetXML::Parser->new(File => $file);
153             $appsettings = $parser->getConfigSection($section);
154             }
155              
156             *{"$package\::$varname"} = $appsettings;
157             }
158              
159             =head1 BUGS
160              
161             Those familiar with the .NET Framework will realise that this is not a
162             complete implementation of all of the facilities offered by the
163             System.Configuration class: this will come later.
164              
165             Some may consider the wanton exporting of names into the calling package
166             to be a bad thing.
167              
168             =head1 SEE ALSO
169              
170             perl, .NET Framework documentation
171              
172             =head1 AUTHOR
173              
174             Jonathan Stowe
175              
176             =head1 COPYRIGHT
177              
178             This library is free software - it comes with no warranty whatsoever.
179              
180             Copyright (c) 2004, 2005 Jonathan Stowe
181              
182             This module can be distributed under the same terms as Perl itself.
183              
184             =cut
185              
186             1;
187