| 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 | <configuration> | ||
| 34 | <appSettings> | ||
| 35 | <add key="msg" value="Bar" /> | ||
| 36 | </appSettings> | ||
| 37 | </configuration> | ||
| 38 | |||
| 39 | The E<lt>addE<gt>elements 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<use Config::DotNetXML> 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<Config::DotNetXML::Parser> for details on named sections. | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | If you don't want or need the import you should use the L<Config::DotNetXML::Parser> | ||
| 72 | module which is part of this package instead. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | 8 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 77 | 8 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 78 | 8 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 79 | 0 | ▶ | use Config::DotNetXML::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 80 | |||
| 81 | BEGIN | ||
| 82 | { | ||
| 83 | delete $INC{'FindBin.pm'}; | ||
| 84 | require FindBin; | ||
| 85 | } | ||
| 86 | |||
| 87 | our $VERSION = '1.6'; | ||
| 88 | |||
| 89 | sub import | ||
| 90 | { | ||
| 91 | |||
| 92 | my ($pkg, %Args ) = @_; | ||
| 93 | |||
| 94 | no strict 'refs'; | ||
| 95 | |||
| 96 | my $suffix = '.config'; | ||
| 97 | |||
| 98 | |||
| 99 | my $package; | ||
| 100 | |||
| 101 | if ( exists $Args{Package} ) | ||
| 102 | { | ||
| 103 | $package = $Args{Package}; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | |||
| 108 | if ( ($package = caller(0)) eq 'Test::More') | ||
| 109 | { | ||
| 110 | $package = caller(1); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | my $varname; | ||
| 116 | |||
| 117 | if ( exists $Args{VarName} ) | ||
| 118 | { | ||
| 119 | $varname = $Args{VarName}; | ||
| 120 | } | ||
| 121 | else | ||
| 122 | { | ||
| 123 | $varname = 'appSettings'; | ||
| 124 | } | ||
| 125 | |||
| 126 | my $file; | ||
| 127 | |||
| 128 | if ( exists $Args{File} ) | ||
| 129 | { | ||
| 130 | $file = $Args{File}; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { | ||
| 134 | $file = File::Spec->catfile($FindBin::Bin,$FindBin::Script) . $suffix; | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | my $section = 'appSettings'; | ||
| 139 | |||
| 140 | if ( exists $Args{Section} ) | ||
| 141 | { | ||
| 142 | $section = $Args{Section} | ||
| 143 | } | ||
| 144 | |||
| 145 | my $parser; | ||
| 146 | |||
| 147 | my $appsettings = {}; | ||
| 148 | if ( -f $file and -r $file ) | ||
| 149 | { | ||
| 150 | $parser = Config::DotNetXML::Parser->new(File => $file); | ||
| 151 | $appsettings = $parser->getConfigSection($section); | ||
| 152 | } | ||
| 153 | |||
| 154 | *{"$package\::$varname"} = $appsettings; | ||
| 155 | } | ||
| 156 | |||
| 157 | =head1 BUGS | ||
| 158 | |||
| 159 | Those familiar with the .NET Framework will realise that this is not a | ||
| 160 | complete implementation of all of the facilities offered by the | ||
| 161 | System.Configuration class: this will come later. | ||
| 162 | |||
| 163 | Some may consider the wanton exporting of names into the calling package | ||
| 164 | to be a bad thing. | ||
| 165 | |||
| 166 | =head1 SEE ALSO | ||
| 167 | |||
| 168 | perl, .NET Framework documentation | ||
| 169 | |||
| 170 | =head1 AUTHOR | ||
| 171 | |||
| 172 | Jonathan Stowe <jns@gellyfish.co.uk> | ||
| 173 | |||
| 174 | =head1 COPYRIGHT | ||
| 175 | |||
| 176 | This library is free software - it comes with no warranty whatsoever. | ||
| 177 | |||
| 178 | Copyright (c) 2004, 2005, 2016 Jonathan Stowe | ||
| 179 | |||
| 180 | This module can be distributed under the same terms as Perl itself. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||