Config-Nested

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

TODO
Nothing planned.

SUGGESTIONS
Send suggestions, bugs, etc. to Anthony+perl@bifb.org.

EXAMPLE CONFIGUARATION FILE

The following data would produce 2 location configuration hashes and 6
animal configuration hashes. Note that variables (e.g. owner) and
booleans (e.g. alive) are scoped to their enclosing block.

location home {
	animal fish 
	{
		name Fred
		sex male
		colour yellow
		happy
		alive
	}

	{
		owner  Gladys

		animal rat
		{
			name Bob
			!alive
		}
		animal bird
		{
			name Clyde
			sex female
			colour yellow
			alive
		}
	}
}

location work
{
	animal horse
	{
		name "A Horse"
	}

examples/parse  view on Meta::CPAN

use Config::Nested;

# Slurp configuration data
$/=undef;
my $conf = <>;

my $c = new Config::Nested();
$conf = $c->autoConfigure($conf); # get configuration from file.

# Parse defaults.
#$c->parse("alive !hungry") || die "Parsing defaults failed\n";

# Parse configuration
$c->parse($conf) || die "Parsing failed\n";

for my $s ($c->sections())
{
	print "================= $s ====================\n";
	my @obj	= $c->section($s);
	print Dumper(\@obj);
}

examples/zoo  view on Meta::CPAN


use Data::Dumper;

use FindBin;
use lib "$FindBin::Bin/lib";

use Config::Nested;

my $c = new Config::Nested(
	section => [qw( location animal)],
	boolean => [qw( happy hungry alive)],
	variable => [qw( sex name colour owner) ],
	array => 'breed exercise',
);

# Slurp configuration data
$/=undef;
my $conf = <DATA>;

# Parse defaults.
$c->parse("alive !hungry") || die "Parsing defaults failed\n";
$c->parse("owner 'Peter the Great' happy") || die "Parsing defaults failed\n";
$c->parse("sex = male") || die "Parsing defaults failed\n";
$c->parse("animal Fred") || die "Parsing defaults failed\n";


# Parse configuration
$c->parse($conf) || die "Parsing failed\n";

for my $s ($c->sections())
{

examples/zoo  view on Meta::CPAN

__DATA__
owner George

location home {
	animal fish 
	{
		name Fred
		sex male
		colour yellow
		happy
		alive
	}

	{
		owner  Gladys

		animal rat
		{
			name Bob
			!alive
		}
		animal bird
		{
			name Clyde
			sex female
			colour yellow
			alive
		}
	}
}

location work
{
	animal horse
	{
		name "A Horse"
		sex male

lib/Config/Nested.pm  view on Meta::CPAN


Config::Nested - parse a configuration file consiging of nested blocks and sections.

=head1 SYNOPSIS

use Config::Nested;
use Data::Dumper;

my $c = new Config::Nested(
    section => [qw( location animal)],
    boolean => [qw( happy hungry alive)],
    variable => [qw( sex name colour ) ],
    array => 'breed exercise owner',
    hash => 'path',
);

$c->parseFile($ARGV[0]) || die "failed to parse!\n";

my @list = $c->section('animal');
print Dumper(\@list;

t/animals.t.conf  view on Meta::CPAN

# Set up the configuration
@section animal location
@array breed exercise
@boolean happy hungry alive
@variable sex name owner
@hash colour
@defaults alive !hungry owner 'Peter the Great'
@defaults happy sex = male

# Configuration file.
owner George

location home {
	animal fish 
	{
		name Fred
		sex male
		colour body yellow
		happy
		alive
	}

	{
		owner  Gladys

		animal rat
		{
			name Bob
			!alive
		}
		animal bird
		{
			name Clyde
			sex female
			colour body green
			alive
		}
	}
}

location work
{
	animal horse
	{
		name "A Horse"
		sex male

t/animals.t.out  view on Meta::CPAN

                                 '+' => [
                                          [
                                            'location',
                                            'home'
                                          ],
                                          [
                                            'animal',
                                            'fish'
                                          ]
                                        ],
                                 'alive' => 1,
                                 'animal' => 'fish',
                                 'breed' => [],
                                 'colour' => {
                                               'body' => 'yellow'
                                             },
                                 'exercise' => [],
                                 'happy' => 1,
                                 'hungry' => 0,
                                 'location' => 'home',
                                 'name' => 'Fred',

t/animals.t.out  view on Meta::CPAN

                                 '+' => [
                                          [
                                            'location',
                                            'home'
                                          ],
                                          [
                                            'animal',
                                            'rat'
                                          ]
                                        ],
                                 'alive' => 0,
                                 'animal' => 'rat',
                                 'breed' => [],
                                 'colour' => {},
                                 'exercise' => [],
                                 'happy' => 1,
                                 'hungry' => 0,
                                 'location' => 'home',
                                 'name' => 'Bob',
                                 'owner' => 'Gladys',
                                 'sex' => 'male'

t/animals.t.out  view on Meta::CPAN

                                 '+' => [
                                          [
                                            'location',
                                            'home'
                                          ],
                                          [
                                            'animal',
                                            'bird'
                                          ]
                                        ],
                                 'alive' => 1,
                                 'animal' => 'bird',
                                 'breed' => [],
                                 'colour' => {
                                               'body' => 'green'
                                             },
                                 'exercise' => [],
                                 'happy' => 1,
                                 'hungry' => 0,
                                 'location' => 'home',
                                 'name' => 'Clyde',

t/animals.t.out  view on Meta::CPAN

                                 '+' => [
                                          [
                                            'location',
                                            'work'
                                          ],
                                          [
                                            'animal',
                                            'horse'
                                          ]
                                        ],
                                 'alive' => 1,
                                 'animal' => 'horse',
                                 'breed' => [],
                                 'colour' => {
                                               'head' => 'brown',
                                               'nose' => 'black'
                                             },
                                 'exercise' => [],
                                 'happy' => 1,
                                 'hungry' => 0,
                                 'location' => 'work',

t/animals.t.out  view on Meta::CPAN

                                 '+' => [
                                          [
                                            'animal',
                                            'dog'
                                          ],
                                          [
                                            'animal',
                                            'flea'
                                          ]
                                        ],
                                 'alive' => 1,
                                 'animal' => 'flea',
                                 'breed' => [],
                                 'colour' => {},
                                 'exercise' => [],
                                 'happy' => 1,
                                 'hungry' => 0,
                                 'location' => '',
                                 'name' => 'Rover',
                                 'owner' => 'George',
                                 'sex' => 'male'
                               }, 'Config::Nested::Section' ),
                        bless( {
                                 '+' => [
                                          [
                                            'animal',
                                            'dog'
                                          ]
                                        ],
                                 'alive' => 1,
                                 'animal' => 'dog',
                                 'breed' => [],
                                 'colour' => {},
                                 'exercise' => [],
                                 'happy' => 1,
                                 'hungry' => 0,
                                 'location' => '',
                                 'name' => 'Rover',
                                 'owner' => 'George',
                                 'sex' => 'male'
                               }, 'Config::Nested::Section' )
                      ],
          'location' => [
                          bless( {
                                   '+' => [
                                            [
                                              'location',
                                              'home'
                                            ]
                                          ],
                                   'alive' => 1,
                                   'animal' => '',
                                   'breed' => [],
                                   'colour' => {},
                                   'exercise' => [],
                                   'happy' => 1,
                                   'hungry' => 0,
                                   'location' => 'home',
                                   'name' => '',
                                   'owner' => 'George',
                                   'sex' => 'male'
                                 }, 'Config::Nested::Section' ),
                          bless( {
                                   '+' => [
                                            [
                                              'location',
                                              'work'
                                            ]
                                          ],
                                   'alive' => 1,
                                   'animal' => '',
                                   'breed' => [],
                                   'colour' => {},
                                   'exercise' => [],
                                   'happy' => 1,
                                   'hungry' => 0,
                                   'location' => 'work',
                                   'name' => '',
                                   'owner' => 'George',
                                   'sex' => 'male'

t/class.t  view on Meta::CPAN

BEGIN { use_ok('Config::Nested'); }

my $c;

ok($c = new Config::Nested(), "constructor");

ok($c->initialise(), "init");

ok($c->configure(
        section => [qw( home work)],
	boolean => [qw( happy hungry alive)],
	variable => [qw( name flavour colour ) ],
	array => 'breed exercise owner',
), "configure");




( run in 1.614 second using v1.01-cache-2.11-cpan-39bf76dae61 )