The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Nour::Config

VERSION

version 0.05

NAME

Nour::Config

ABSTRACT

Recursively consumes YAML configuration from the config directory into a hash ref for use in your application.

USAGE EXAMPLE

1. Create a config directory, either config, conf, or cfg.
  you@your_computer:~/code/your_app $ mkdir config
  you@your_computer:~/code/your_app $ mkdir config/application
  you@your_computer:~/code/your_app $ mkdir config/database
  you@your_computer:~/code/your_app $ mkdir config/database/private
  you@your_computer:~/code/your_app $ mkdir -p config/a/deeply/nested/example
2. Create your configuration YAML files
  you@your_computer:~/code/your_app $ echo '---' > config/config.yml
  you@your_computer:~/code/your_app $ echo '---' > config/application/config.yml
  you@your_computer:~/code/your_app $ echo '---' > config/database/config.yml
  you@your_computer:~/code/your_app $ echo '---' > config/database/private/config.yml
  you@your_computer:~/code/your_app $ echo '---' > config/a/deeply/nested/example/neato.yml
3. Edit your configuration YAML with whatever you want.
4. In your script or application, create a Nour::Config instance.
  use Nour::Config;
  use Data::Dumper;
  use feature ':5.10';

then

  # automatically detects and reads from a config, conf, or cfg directory
  my $config = new Nour::Config;

or

  my $config = new Nour::Config (
    -base => 'config/application'
  );

or

  my $config = new Nour::Config (
      -conf => { hash_key => 'override' }
    , -base => 'config'
  );

or

  my $config = new Nour::Config (
    this_becomes_a_hash_key => 'config/database'
  );

finally

    say 'cfg', Dumper $config->config;
    say 'app', Dumper $config->config( 'application' );
    say 'db',  Dumper $config->config->{database};

But it's even better with Moose if you import the config handle, so you can use config as a handle in your script or application:

    use Moose;
    use Nour::Config;
    use Data::Dumper;

    has _config => (
        is => 'rw'
        , isa => 'Nour::Config'
        , handles => [ qw/config/ ]
        , required => 1
        , lazy => 1
        , default => sub {
            return new Nour::Config ( -base => 'config' );
        }
    );

    sub BUILD {
        my $self = shift;

        print "\nhello world\n", Dumper( $self->config ), "\n";
    }

METHODS

config

Returns the configuration accessor, and doubles as a hash ref.

  print "\n", Dumper( $self->config( 'application' ) ), "\n";
  print "\n", Dumper( $self->config->{application} ), "\n";

AUTHOR

Nour Sharabash <amirite@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Nour Sharabash.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.