line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::IniSearch; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
830
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
1
|
|
|
1
|
|
15
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
6
|
1
|
|
|
1
|
|
5
|
use Cwd; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
141
|
|
7
|
1
|
|
|
1
|
|
411
|
use Config::IniHash; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Exporter; |
10
|
|
|
|
|
|
|
our @ISA = qw( Exporter ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
our $GLOBAL_INI = "global.ini"; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Config::IniSearch - Wrapper class for Config::IniHash to search for INI files |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use Config::IniSearch; |
22
|
|
|
|
|
|
|
$config = new Config::IniSearch( 'inisection' ); |
23
|
|
|
|
|
|
|
$val = $config->{param_name}; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Config::IniSearch; |
26
|
|
|
|
|
|
|
$config = new Config::IniSearch( 'inisection', ['/usr/local/share/myfile.ini'] ); |
27
|
|
|
|
|
|
|
$val = $config->{param_name}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This module is intended to relieve the developer from having to keep |
32
|
|
|
|
|
|
|
track of the location and name of individual INI files. An INI file |
33
|
|
|
|
|
|
|
will be recognized by one of two names: |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=over 4 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item inisection.ini |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item $GLOBAL_INI.ini |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=back |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Either/both of these must contain the section [inisection]. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
The following methods are available: |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=over 4 |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item $config = new Config::IniSearch( $INISectionName, [$INIFileName, %IniHashOpts] ); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
The constructor requires one argument, a section name, |
52
|
|
|
|
|
|
|
$INISectionName. Optional arguments include a fully-qualified INI |
53
|
|
|
|
|
|
|
filename ($INIFileName) and options for Config::IniHash (see perldoc |
54
|
|
|
|
|
|
|
Config::IniHash). NOTE: By default, case is preserved. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$INIFileName must be set if passing in options for |
57
|
|
|
|
|
|
|
Config::IniHash. The section name does not need to be specified when |
58
|
|
|
|
|
|
|
accessing elements in $INISectionName: |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$val = $config->{param_name}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
When the constructor is invoked, the following list of defaults is |
63
|
|
|
|
|
|
|
searched for the appropriate INI file: |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub new { |
68
|
|
|
|
|
|
|
my $class = shift; |
69
|
|
|
|
|
|
|
return undef if( $#_ < 0 ); |
70
|
|
|
|
|
|
|
my $self = bless {}, $class; |
71
|
|
|
|
|
|
|
my $section = $self->{__section__} = shift; |
72
|
|
|
|
|
|
|
$self->{__iniFile__} = shift; |
73
|
|
|
|
|
|
|
my %iniHashOpts = @_; |
74
|
|
|
|
|
|
|
$iniHashOpts{case} = 'preserve' if( ! $iniHashOpts{case} ); |
75
|
|
|
|
|
|
|
_searchDirs $self if( ! $self->{__iniFile__} ); |
76
|
|
|
|
|
|
|
return undef if( ! $self->{__iniFile__} ); |
77
|
|
|
|
|
|
|
my $config = ReadINI( $self->{__iniFile__}, %iniHashOpts ); |
78
|
|
|
|
|
|
|
%$self = map { $_, $config->{$section}->{$_} } keys %{$config->{$section}}; |
79
|
|
|
|
|
|
|
return $self; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=over 4 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item $INIFileName.ini |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item $cwd/$INISectionName.ini |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item $cwd/$GLOBAL_INI |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item scriptdir/$INISectionName.ini |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item scriptdir/$GLOBAL_INI |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item /etc/$INISectionName.ini |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item /etc/$GLOBAL_INI |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Notice that the first INI file found will be the one used for |
99
|
|
|
|
|
|
|
subsequent lookups. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=back |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _searchDirs { |
106
|
|
|
|
|
|
|
my $self = shift; |
107
|
|
|
|
|
|
|
return undef if( ! $self ); |
108
|
|
|
|
|
|
|
my $cwd = cwd(); |
109
|
|
|
|
|
|
|
return $self->{__iniFile__} = "$cwd/$self->{__section__}.ini" |
110
|
|
|
|
|
|
|
if( -r "$cwd/$self->{__section__}.ini" ); |
111
|
|
|
|
|
|
|
return $self->{__iniFile__} = "$cwd/$GLOBAL_INI" |
112
|
|
|
|
|
|
|
if( -r "$cwd/$GLOBAL_INI" ); |
113
|
|
|
|
|
|
|
my $baseDir = _getBaseDir( $0 ); |
114
|
|
|
|
|
|
|
return undef if( ! $baseDir ); |
115
|
|
|
|
|
|
|
return $self->{__iniFile__} = "$baseDir/$self->{__section__}.ini" |
116
|
|
|
|
|
|
|
if( -r "$baseDir/$self->{__section__}.ini" ); |
117
|
|
|
|
|
|
|
return $self->{__iniFile__} = "$baseDir/$GLOBAL_INI" |
118
|
|
|
|
|
|
|
if( -r "$baseDir/$GLOBAL_INI" ); |
119
|
|
|
|
|
|
|
return $self->{__iniFile__} = "/etc/$self->{__section__}.ini" |
120
|
|
|
|
|
|
|
if( -r "/etc/$self->{__section__}.ini" ); |
121
|
|
|
|
|
|
|
return $self->{__iniFile__} = "/etc/$GLOBAL_INI" |
122
|
|
|
|
|
|
|
if( -r "/etc/$GLOBAL_INI" ); |
123
|
|
|
|
|
|
|
return undef; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _getBaseDir { |
127
|
|
|
|
|
|
|
$_ = shift; |
128
|
|
|
|
|
|
|
return "./" if( ! /\/+/ ); |
129
|
|
|
|
|
|
|
/(.*)\/(.*)/; |
130
|
|
|
|
|
|
|
return $1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=over 4 |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item $inifile = $config->getINIFilename; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Returns the name of the INI file currently accessed by the object. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub getINIFilename { |
142
|
|
|
|
|
|
|
my $self = shift; |
143
|
|
|
|
|
|
|
return $self->{__iniFile__}; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item $section_name = $config->getSectionName; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Returns the name of the INI section name currently accessed by |
149
|
|
|
|
|
|
|
$config. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub getSectionName { |
154
|
|
|
|
|
|
|
my $self = shift; |
155
|
|
|
|
|
|
|
return $self->{__section__}; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=back |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 EXPORT |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
None by default. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 AUTHOR |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Brian Koontz |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Copyright (c) 2003 Brian Koontz . |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
173
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |