Config::Context::ConfigGeneral - Use Config::General (Apache-style) config files with Config::Context


Config-Context documentation Contained in the Config-Context distribution.

Index


Code Index:

NAME

Top

Config::Context::ConfigGeneral - Use Config::General (Apache-style) config files with Config::Context

SYNOPSIS

Top

    use Config::Context;

    my $config_text = '

        <Location /users>
            title = "User Area"
        </Location>

        <LocationMatch \.*(jpg|gif|png)$>
            image_file = 1
        </LocationMatch>

    ';

    my $conf = Config::Context->new(
        string        => $config_text,
        driver        => 'ConfigGeneral',
        match_sections => [
            {
                name          => 'Location',
                match_type    => 'path',
            },
            {
                name          => 'LocationMatch',
                match_type    => 'regex',
            },
        ],
    );

    my %config = $conf->context('/users/~mary/index.html');

    use Data::Dumper;
    print Dumper(\%config);
    --------
    $VAR1 = {
        'title'         => 'User Area',
        'image_file'    => undef,
    };

    my %config = $conf->getall_matching('/users/~biff/images/flaming_logo.gif');
    print Dumper(\%config);
    --------
    $VAR1 = {
        'title'         => 'User Area',
        'image_file'    => 1,
    };

DESCRIPTION

Top

This module uses Config::General to parse Apache-style config files for Config::Context. See the Config::Context docs for more information.

DEFAULT OPTIONS

Top

In addition to the options normally enabled by Config::Scoped, the following options are turned on by default:

    -MergeDuplicateBlocks  => 1
    -MergeDuplicateOptions => 1
    -IncludeRelative       => 1

You can change this behaviour by passing a different value to driver_params to new:

    my $conf = Config::Context->new(
        driver => 'ConfigGeneral',
        driver_options => {
           ConfigGeneral = > {
               -MergeDuplicateBlocks  => 0,
           },
        },
    );




CONSTRUCTOR

Top

new(...)

    my $driver = Config::Context::ConfigGeneral->new(
        file             => $config_file,
        lower_case_names => 1,  # optional
        options          => {
            # ...
        }
    );

or:

    my $driver = Config::Context::ConfigGeneral->new(
        string           => $config_string,
        lower_case_names => 1,  # optional
        options          => {
            # ...
        }
    );

Returns a new driver object, using the provided options.

METHODS

Top

parse()

Returns the data structure for the parsed config.

files()

Returns a list of all the config files read, including any config files included in the main file.

config_modules

Returns the modules used to parse the config. In this case: Config::General

CAVEATS

Top

Don't quote block names

Instead of:

    <Location '/foo'>
    </Location>

Use:

    <Location /foo>
    </Location>

SEE ALSO

Top

    Config::Context
    CGI::Application::Plugin::Config::Context
    Config::General

COPYRIGHT & LICENSE

Top


Config-Context documentation Contained in the Config-Context distribution.
package Config::Context::ConfigGeneral;

use warnings;
use strict;

use Carp;
use Cwd;

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;
    my %args  = @_;

    Config::Context->_require_prerequisite_modules($class);

    my %driver_opts = %{ $args{'options'}{'ConfigGeneral'} || {} };

    $driver_opts{'-MergeDuplicateBlocks'} = 1
        unless defined $driver_opts{'-MergeDuplicateBlocks'};

    $driver_opts{'-MergeDuplicateOptions'} = 1
        unless defined $driver_opts{'-MergeDuplicateOptions'};

    $driver_opts{'-IncludeRelative'} = 1
        unless defined $driver_opts{'-IncludeRelative'};


    $driver_opts{'-LowerCaseNames'} = $args{'lower_case_names'};

    my $self = {};

    if ($args{'string'}) {
        local $^W; # suppress 'uninitialized value' warnings from within Config::General

        $self->{'conf'} = Config::General->new(
            %driver_opts,
            -String  => $args{'string'},
        );
    }
    elsif($args{'file'}) {
        local $^W; # suppress 'uninitialized value' warnings from within Config::General

        $self->{'conf'} = Config::General->new(
            %driver_opts,
            -ConfigFile => $args{'file'},
        );
        $self->{'file'} = $args{'file'};
    }
    else {
        croak __PACKAGE__ . "->new(): one of 'file' or 'string' is required";
    }

    bless $self, $class;
    return $self;

}

sub parse {
    my $self = shift;
    my %config = $self->{'conf'}->getall;
    return %config if wantarray;
    return \%config;
}

sub files {
    my $self = shift;

    my @files;
    if ($self->{'conf'}->can('files')) {
        @files = $self->{'conf'}->files;
    }
    elsif (exists $self->{'file'}) {
        @files = ($self->{'file'});
    }

    @files = map { Cwd::abs_path($_) } @files;

    return @files if wantarray;
    return \@files;
}

sub config_modules {
    'Config::General';
}

1;