| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Config::Context::ConfigGeneral; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 17 |  |  | 17 |  | 132568 | use warnings; | 
|  | 17 |  |  |  |  | 35 |  | 
|  | 17 |  |  |  |  | 689 |  | 
| 4 | 17 |  |  | 17 |  | 95 | use strict; | 
|  | 17 |  |  |  |  | 30 |  | 
|  | 17 |  |  |  |  | 583 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 17 |  |  | 17 |  | 92 | use Carp; | 
|  | 17 |  |  |  |  | 33 |  | 
|  | 17 |  |  |  |  | 1542 |  | 
| 7 | 17 |  |  | 17 |  | 109 | use Cwd; | 
|  | 17 |  |  |  |  | 32 |  | 
|  | 17 |  |  |  |  | 10959 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | =head1 NAME | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | Config::Context::ConfigGeneral - Use Config::General (Apache-style) config files with Config::Context | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | use Config::Context; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | my $config_text = ' | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | title = "User Area" | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | image_file = 1 | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | '; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my $conf = Config::Context->new( | 
| 30 |  |  |  |  |  |  | string        => $config_text, | 
| 31 |  |  |  |  |  |  | driver        => 'ConfigGeneral', | 
| 32 |  |  |  |  |  |  | match_sections => [ | 
| 33 |  |  |  |  |  |  | { | 
| 34 |  |  |  |  |  |  | name          => 'Location', | 
| 35 |  |  |  |  |  |  | match_type    => 'path', | 
| 36 |  |  |  |  |  |  | }, | 
| 37 |  |  |  |  |  |  | { | 
| 38 |  |  |  |  |  |  | name          => 'LocationMatch', | 
| 39 |  |  |  |  |  |  | match_type    => 'regex', | 
| 40 |  |  |  |  |  |  | }, | 
| 41 |  |  |  |  |  |  | ], | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | my %config = $conf->context('/users/~mary/index.html'); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | use Data::Dumper; | 
| 47 |  |  |  |  |  |  | print Dumper(\%config); | 
| 48 |  |  |  |  |  |  | -------- | 
| 49 |  |  |  |  |  |  | $VAR1 = { | 
| 50 |  |  |  |  |  |  | 'title'         => 'User Area', | 
| 51 |  |  |  |  |  |  | 'image_file'    => undef, | 
| 52 |  |  |  |  |  |  | }; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | my %config = $conf->getall_matching('/users/~biff/images/flaming_logo.gif'); | 
| 55 |  |  |  |  |  |  | print Dumper(\%config); | 
| 56 |  |  |  |  |  |  | -------- | 
| 57 |  |  |  |  |  |  | $VAR1 = { | 
| 58 |  |  |  |  |  |  | 'title'         => 'User Area', | 
| 59 |  |  |  |  |  |  | 'image_file'    => 1, | 
| 60 |  |  |  |  |  |  | }; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | This module uses C to parse Apache-style config files for | 
| 65 |  |  |  |  |  |  | C.  See the C docs for more information. | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head1 DEFAULT OPTIONS | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | In addition to the options normally enabled by Config::Scoped, the | 
| 70 |  |  |  |  |  |  | following options are turned on by default: | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | -MergeDuplicateBlocks  => 1 | 
| 73 |  |  |  |  |  |  | -MergeDuplicateOptions => 1 | 
| 74 |  |  |  |  |  |  | -IncludeRelative       => 1 | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | You can change this behaviour by passing a different value to | 
| 77 |  |  |  |  |  |  | C to C: | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | my $conf = Config::Context->new( | 
| 80 |  |  |  |  |  |  | driver => 'ConfigGeneral', | 
| 81 |  |  |  |  |  |  | driver_options => { | 
| 82 |  |  |  |  |  |  | ConfigGeneral = > { | 
| 83 |  |  |  |  |  |  | -MergeDuplicateBlocks  => 0, | 
| 84 |  |  |  |  |  |  | }, | 
| 85 |  |  |  |  |  |  | }, | 
| 86 |  |  |  |  |  |  | ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head1 CONSTRUCTOR | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =head2 new(...) | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | my $driver = Config::Context::ConfigGeneral->new( | 
| 94 |  |  |  |  |  |  | file             => $config_file, | 
| 95 |  |  |  |  |  |  | lower_case_names => 1,  # optional | 
| 96 |  |  |  |  |  |  | options          => { | 
| 97 |  |  |  |  |  |  | # ... | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | or: | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | my $driver = Config::Context::ConfigGeneral->new( | 
| 104 |  |  |  |  |  |  | string           => $config_string, | 
| 105 |  |  |  |  |  |  | lower_case_names => 1,  # optional | 
| 106 |  |  |  |  |  |  | options          => { | 
| 107 |  |  |  |  |  |  | # ... | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | ); | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Returns a new driver object, using the provided options. | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =cut | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub new { | 
| 116 | 27 |  |  | 27 | 1 | 73 | my $proto = shift; | 
| 117 | 27 |  | 33 |  |  | 154 | my $class = ref $proto || $proto; | 
| 118 | 27 |  |  |  |  | 162 | my %args  = @_; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 27 |  |  |  |  | 146 | Config::Context->_require_prerequisite_modules($class); | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 27 | 100 |  |  |  | 43 | my %driver_opts = %{ $args{'options'}{'ConfigGeneral'} || {} }; | 
|  | 27 |  |  |  |  | 221 |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 27 | 50 |  |  |  | 136 | $driver_opts{'-MergeDuplicateBlocks'} = 1 | 
| 125 |  |  |  |  |  |  | unless defined $driver_opts{'-MergeDuplicateBlocks'}; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 27 | 50 |  |  |  | 95 | $driver_opts{'-MergeDuplicateOptions'} = 1 | 
| 128 |  |  |  |  |  |  | unless defined $driver_opts{'-MergeDuplicateOptions'}; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 27 | 50 |  |  |  | 89 | $driver_opts{'-IncludeRelative'} = 1 | 
| 131 |  |  |  |  |  |  | unless defined $driver_opts{'-IncludeRelative'}; | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 27 |  |  |  |  | 64 | $driver_opts{'-LowerCaseNames'} = $args{'lower_case_names'}; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 27 |  |  |  |  | 94 | my $self = {}; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 27 | 100 |  |  |  | 107 | if ($args{'string'}) { | 
|  |  | 50 |  |  |  |  |  | 
| 139 | 15 |  |  |  |  | 72 | local $^W; # suppress 'uninitialized value' warnings from within Config::General | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 15 |  |  |  |  | 140 | $self->{'conf'} = Config::General->new( | 
| 142 |  |  |  |  |  |  | %driver_opts, | 
| 143 |  |  |  |  |  |  | -String  => $args{'string'}, | 
| 144 |  |  |  |  |  |  | ); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | elsif($args{'file'}) { | 
| 147 | 12 |  |  |  |  | 51 | local $^W; # suppress 'uninitialized value' warnings from within Config::General | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 12 |  |  |  |  | 123 | $self->{'conf'} = Config::General->new( | 
| 150 |  |  |  |  |  |  | %driver_opts, | 
| 151 |  |  |  |  |  |  | -ConfigFile => $args{'file'}, | 
| 152 |  |  |  |  |  |  | ); | 
| 153 | 11 |  |  |  |  | 15457 | $self->{'file'} = $args{'file'}; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  | else { | 
| 156 | 0 |  |  |  |  | 0 | croak __PACKAGE__ . "->new(): one of 'file' or 'string' is required"; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 | 26 |  |  |  |  | 35594 | bless $self, $class; | 
| 160 | 26 |  |  |  |  | 151 | return $self; | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head1 METHODS | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =head2 parse() | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | Returns the data structure for the parsed config. | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =cut | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | sub parse { | 
| 173 | 26 |  |  | 26 | 1 | 44 | my $self = shift; | 
| 174 | 26 |  |  |  |  | 188 | my %config = $self->{'conf'}->getall; | 
| 175 | 26 | 50 |  |  |  | 347 | return %config if wantarray; | 
| 176 | 26 |  |  |  |  | 416 | return \%config; | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | =head2 files() | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | Returns a list of all the config files read, including any config files | 
| 182 |  |  |  |  |  |  | included in the main file. | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =cut | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | sub files { | 
| 187 | 11 |  |  | 11 | 1 | 20 | my $self = shift; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 11 |  |  |  |  | 18 | my @files; | 
| 190 | 11 | 50 |  |  |  | 81 | if ($self->{'conf'}->can('files')) { | 
|  |  | 0 |  |  |  |  |  | 
| 191 | 11 |  |  |  |  | 44 | @files = $self->{'conf'}->files; | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  | elsif (exists $self->{'file'}) { | 
| 194 | 0 |  |  |  |  | 0 | @files = ($self->{'file'}); | 
| 195 |  |  |  |  |  |  | } | 
| 196 |  |  |  |  |  |  |  | 
| 197 | 11 |  |  |  |  | 103 | @files = map { Cwd::abs_path($_) } @files; | 
|  | 14 |  |  |  |  | 813 |  | 
| 198 |  |  |  |  |  |  |  | 
| 199 | 11 | 50 |  |  |  | 33 | return @files if wantarray; | 
| 200 | 11 |  |  |  |  | 40 | return \@files; | 
| 201 |  |  |  |  |  |  | } | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | =head2 config_modules | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | Returns the modules used to parse the config.  In this case: C | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | =cut | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | sub config_modules { | 
| 210 | 44 |  |  | 44 | 1 | 1309 | 'Config::General'; | 
| 211 |  |  |  |  |  |  | } | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =head1 CAVEATS | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | =head2 Don't quote block names | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | Instead of: | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | Use: | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | Config::Context | 
| 230 |  |  |  |  |  |  | CGI::Application::Plugin::Config::Context | 
| 231 |  |  |  |  |  |  | Config::General | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | Copyright 2004-2005 Michael Graham, All Rights Reserved. | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 238 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | =cut | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | 1; | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  |  |